JSON 来自 URL 未打印 android

JSON from URL not being printed android

所以我有一个 url,我从中得到 json,看起来一切正常(我得到 200 响应代码),但是,当我尝试打印整个 json,它打印

I/System.out﹕ java.io.BufferedInputStream@6cf1c21

代码如下:

 URL url = "myurl.com";
    try {
        url = new URL("url");
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    HttpURLConnection conn = null;
    try {
        conn = (HttpURLConnection) url.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        conn.setRequestMethod("GET");
    } catch (ProtocolException e) {
        e.printStackTrace();
    }

    try {
        System.out.println("Response Code: " + conn.getResponseCode());
    } catch (IOException e) {
        e.printStackTrace();
    }
    InputStream in = null;
    try {
        in = new BufferedInputStream(conn.getInputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(in.toString());

I/System.out﹕ java.io.BufferedInputStream@6cf1c21Object class 的默认 toString() 实现的输出。你应该阅读 pass the stream to a reader,像这样:

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder out = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
        out.append(line);
}
System.out.println(out.toString());

这是你的最后一行!

而不是

System.out.println(in.toString());

尝试

String s = " ";

while(in.ready()){
    s += in.readLine();
}

in.close();
System.out.println(s);