繁体   English   中英

无法从 URL 捕获 JSON 响应

[英]Unable to catch JSON response from URL

我试图发出一个返回 JSON 格式响应的请求。但输出似乎不是 json 格式。请帮忙。

    String url = "http://httpbin.org/ip";
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    // optional default is GET
    con.setRequestMethod("GET");
    //add request header
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);
    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print in String
    System.out.println(response.toString());

    //Read JSON response and print
    JSONObject myResponse = new JSONObject(response.toString());

似乎链接已损坏,因此输出错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM