无法从URLConnection

时间:2016-06-16 11:59:32

标签: java json httprequest urlconnection

我正在尝试使用java URLConnection阅读googleapi的回复。

在浏览器中,如果我查询 " https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=https%3A%2F%2Fwww.zauq.se&strategy=mobilez"

我在JSON格式中得到回应。

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalidParameter",
    "message": "Invalid string value: 'mobilez'. Allowed values: [desktop, mobile]",
    "locationType": "parameter",
    "location": "strategy"
   }
  ],
  "code": 400,
  "message": "Invalid string value: 'mobilez'. Allowed values: [desktop, mobile]"
 }
}

但是当我使用以下代码时。

URL url = null;
            System.setProperty("http.agent", "");

            url = new URL("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=https%3A%2F%2Fwww.zauq.se&strategy=mobilez");//"https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=" + paramUrl + "&strategy=" + paramStrategy);
            URLConnection conn = url.openConnection();
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("User-Agent", "Mozilla/5.0");
            BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String strTemp = "";
            StringBuilder sb = new StringBuilder();

            while ((null != (strTemp = br.readLine())))
            {
                sb.append(strTemp);
                if (dump)
                {
                    System.out.println(strTemp);
                }
            }

我得到IO异常

java.io.IOException: Server returned HTTP response code: 400 for URL: https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=https%3A%2F%2Fwww.zauq.se&strategy=mobilez
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at amirTest.PageSpeedCheck.main(PageSpeedCheck.java:62)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

/但我需要获得JSON响应。

请建议我将JSON作为回应进行更改。

5 个答案:

答案 0 :(得分:3)

将连接转换为Http(s)URLConnection,并在出现错误时调用getErrorStream()以获取流。

答案 1 :(得分:1)

答案 2 :(得分:0)

API调用中使用的参数错误,因此服务器重新调整状态代码400.这意味着它是一个BAD请求。

mobilz应该是移动的。您使用的网址也不正确。

答案 3 :(得分:0)

您只需更改代码如下:

    BufferedReader reader = new BufferedReader(new InputStreamReader(((HttpURLConnection) (new URL("https://www.googleapis.com/pagespeedonline/v2/runPagespeed?url=https%3A%2F%2Fwww.zauq.se&strategy=mobilez")).openConnection())
.getInputStream(), Charset.forName("UTF-8")));

请参阅原始答案here

请参阅以下类似的SO问题:

URLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL

Server returned HTTP response code: 400

答案 4 :(得分:0)

谢谢@ JB Nizet

当我尝试阅读错误流时,它起了作用。

            InputStream is = null;
            if (conn.getResponseCode() == 400)
            {

                is = conn.getErrorStream();
                error = true;
            } else
            {
                is = conn.getInputStream();
            }