如何阅读完整的输出?

时间:2016-07-18 11:38:56

标签: java android json string httpurlconnection

我必须解析this。我将retreived url响应存储在缓冲区中。但是当我将接收到的缓冲区移动到字符串时,我得到一个损坏的输出,所以我无法解析它。但是,当我打印变量line时,我看到收到了整个响应。是字符串的某种缓冲区溢出?我如何解析响应,因为它还包含一个包装函数,所以我不认为我甚至可以使用JSONReader!

这是代码 -

URL url = new URL("http://chartapi.finance.yahoo.com/instrument/1.0/FB/chartdata;type=quote;range=1d/json");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
     InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
     StringBuffer buffer = new StringBuffer();
     if (inputStream == null) return;

     reader = new BufferedReader(new InputStreamReader(inputStream));

     String line;
     while ((line = reader.readLine()) != null) {
     //Log.d(TAG,line);
     buffer.append(line + "\n");
     }

     Log.d(TAG,buffer.length() +buffer.toString());
}

1 个答案:

答案 0 :(得分:0)

你的代码,内部没有任何System.out.println,而正确打印内容(很奇怪),链接你传递似乎 JSON格式在开头你有档案

  

finance_charts_json_callback(

,它不是JSON字符串的一部分。

为了获得正确的数据,快速解决方法是忽略此字符和最后的括号:

String jsonString = buffer.substring(30, buffer.length() - 2);
JsonObject jobj = new Gson().fromJson(jsonString, JsonObject.class);

现在你有一个JSonObject,所以你可以通过属性名称来引用它:

System.out.println(jobj.get("Timestamp").toString());
System.out.println(jobj.get("labels").toString());
System.out.println(jobj.get("ranges").toString());
System.out.println(jobj.get("series").toString());

输出(缩小)

{"min":1468589400,"max":1468612800}
[1468591200,1468594800,1468598400,1468602000,1468605600,1468609200,1468612800]
{"close":{"min":116.6050,"max":118.1800},"high":{"min":116.6562,"max":118.2800},"low":{"min":116.5800,"max":118.1600},"open":{"min":116.6000,"max":118.1786},"volume":{"min":100,"max":1390500}}
[{"Timestamp":1468589424,"close":117.8742,"high":117.9000,"low":117.8050,"open":117.8300,"volume":1390500},{"Timestamp":1468589460,"close":117.8350,"high":117.9900,"low":117.8200,"open":117.8850,"volume":112100},{"Timestamp":1468589578,"close":117.7300,"high":117.8200,"low":117.7000,"open":117.8200,"volume":70600},{"Timestamp":1468589580,"close":117.9770,"high":117.9900,"low":117.7100,"open":117.7600,"volume":91000},{"Timestamp":1468589698,"close":118.0650,"high":118.1000,"low":117.9700,"open":117.9700,"volume":353000},{"Timestamp":1468589701,"close":118.0250,"high":118.0900,"low":117.9210,"open":118.0735,"volume":108400},{"Timestamp":1468589819,"close":118.0900,"high":118.0900,"low":117.9500,"open":118.0201,"volume":121400},{"Timestamp":1468589879,"close":118.1800,"high":118.2300,"low":118.0900,"open":118.0900,"volume":186000},{"Timestamp":1468589939,"close":118.1700,"high":118.1900,"low":118.0700,"open":118.1764,"volume":84400},{"Timestamp":1468589940,"close":118.1800,"high":118.2800,"low":118.1600,"open":118.1786,"volume":152900},{"Timestamp":1468590000,"close":118.1300,"high":118.2100,"low":118.1100,"open":118.1600,"volume":53800},{"Timestamp":1468590060,"close":118.1800,"high":118.1800,"low":118.0950,"open":118.1200,"volume":91800},{"Timestamp":1468590120,"close":118.1110,"high":118.2200,"low":118.0707,"open":118.1700,"volume":65700},{"Timestamp":1468590239,"close":118.1300,"high":118.1400,"low":118.0179,"open":118.1400,"volume":57800},{"Timestamp":1468590240,"close":118.0500,"high":118.1800,"low":118.0500,"open":118.1100,"volume":75000},{"Timestamp":1468590300,"close":118.1000,"high":118.1500,"low":118.0250,"open":118.0449,"volume":51600},{"Timestamp":1468590360,"close":117.9600,"high":118.1100,"low":117.9000,"open":118.1000,"volume":99200},{"Timestamp":1468590421,"close":117.8400,"high":117.9700,"low":117.8200,"open":117.9600,"volume":173800},{"Timestamp":1468590480,"close":117.8800,"high":117.8800,"low":117.8200,"open":117.8300,"volume":92700},{"Timestamp":1468590541,"close":117.7000,"high":117.8850,"low":117.6810,"open":117.8850,"volume":114600},{"Timestamp":1468590659,"close":117.6865,"high":117.7200,"low":117.6600,"open":117.6800,"volume":83200},{"Timestamp":1468590719,"close":117.6300,"high":117.6900,"low":117.4900,"open":117.6800,"volume":121500}, (more results!!!)

完整代码:

public static void main(String[] args) throws Exception {
    URL url = new URL("http://chartapi.finance.yahoo.com/instrument/1.0/FB/chartdata;type=quote;range=1d/json");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
    StringBuffer buffer = new StringBuffer();
    if (inputStream == null)
        return;

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line;

    while ((line = reader.readLine()) != null) {
        buffer.append(line + "\n");
    }

    String jsonString = buffer.substring(30, buffer.length() - 2);
    JsonObject jobj = new Gson().fromJson(jsonString, JsonObject.class);
    System.out.println(jobj.get("Timestamp").toString());
    System.out.println(jobj.get("labels").toString());
    System.out.println(jobj.get("ranges").toString());
    System.out.println(jobj.get("series").toString());

}