2016-04-03 9 views
0

私はこのコードをget Jsonに使用しますが、FileNotFoundExceptionを返します。ブラウザではうまくいきます。なぜそれがJavaで動作しないのかわかりません... 私はHttpsUrlConnectionを使用しようとしましたが、結果は変更されません。Google books filenotfound java

String url = "https://www.googleapis.com/books/v1/volumes"; 
    HashMap<String, String> params = new HashMap<String, String>(); 
    params.put("q", "isbn:9785090212779"); 
    StringBuilder postData = new StringBuilder(); 
    if (params != null) 
     for (Map.Entry<String, String> param : params.entrySet()) { 
      if (postData.length() != 0) postData.append('&'); 
      postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); 
      postData.append('='); 
      postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); 
     } 
    byte[] postDataBytes = postData.toString().getBytes("UTF-8"); 

    HttpsURLConnection urlc = (HttpsURLConnection) 
      new URL(url).openConnection(); 
    urlc.setDoOutput(true); 
    urlc.setRequestMethod("GET"); 
    urlc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
    urlc.setRequestProperty("charset", "UTF-8"); 
    urlc.setRequestProperty("Content-Length", Integer.toString(postDataBytes.length)); 
    DataOutputStream wr; 
    wr = new DataOutputStream(urlc.getOutputStream()); 
    wr.write(postDataBytes); 
    wr.flush(); 
    wr.close(); 


    urlc.setConnectTimeout(2000); 
    DataInputStream is = new DataInputStream(urlc.getInputStream()); 
    BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 
    String line; 
    while ((line = br.readLine()) != null) { 
     sb.append(line); 
    } 
    br.close(); 
    urlc.disconnect(); 
    System.out.println(sb.toString()); 

例外:https://console.developers.google.com/apis/

Exception in thread "main" java.io.FileNotFoundException: https://www.googleapis.com/books/v1/volumes 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1836) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) 
    at Main.main(Main.java:43) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 
+0

リンクに記載されているように適切な変更を加えることができます。http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/ – AbhishekAsh

+0

テスト2 - HTTP POSTリクエストを送信 スレッド "main"の例外java.io.FileNotFoundException:https://selfsolve.apple.com/wcResults.do \t at sun.reflect.NativeConstructorAccessorImpl.newInstance0(ネイティブ –

答えて

0

Googleブックスのみ要求getを使用あなたがここにあなたのキーを取得query.You FPRはGoogle API-キーを含める必要が

0

最後にリンクは次のようになります:

https://www.googleapis.com/books/v1/volumes?q=isbn=9783446440739&key=HERE_IS_YOUR_API_KEY 
関連する問題