Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GoogleBooksFileNotFoundJava_Java_Post_Https_Google Books - Fatal编程技术网

GoogleBooksFileNotFoundJava

GoogleBooksFileNotFoundJava,java,post,https,google-books,Java,Post,Https,Google Books,我使用此代码获取Json,但它返回FileNotFoundException。在browset中,它可以工作。我不明白为什么它不能在Java中工作。。。 我尝试使用HttpsUrlConnection,但结果不变 String url = "https://www.googleapis.com/books/v1/volumes"; HashMap<String, String> params = new HashMap<String, String>();

我使用此代码获取Json,但它返回FileNotFoundException。在browset中,它可以工作。我不明白为什么它不能在Java中工作。。。 我尝试使用HttpsUrlConnection,但结果不变

   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());

Google Books仅使用GET请求

您必须在查询中包含您的Google Api密钥。您可以在此处获取密钥:

最后,您的链接应如下所示:

https://www.googleapis.com/books/v1/volumes?q=isbn=9783446440739&key=HERE_IS_YOUR_API_KEY

您可以执行以下链接中提到的适当更改:Testing 2-在线程“main”java.io.FileNotFoundException:at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native)中发送Http POST请求异常
https://www.googleapis.com/books/v1/volumes?q=isbn=9783446440739&key=HERE_IS_YOUR_API_KEY