Jack's retarded code Jack's retarded code - 1 year ago 57
Java Question

How to read the complete output?

I have to parse this. I am storing the retreived url response in a buffer.But when I move the received buffer to a string I get a broken output and so I am unable to parse it. However when I print the variable

line
, I see the entire response is received. Is it some sort of buffer overflow for string? How can I parse the response, as it also contains a wrapper function and so I dont think I can even use the JSONReader!

Here is the code-

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

Answer Source

Your code, without any System.out.println inside while is printing the content correctly (weird), link you passed seems JSON format BUT in the start of the file you have

finance_charts_json_callback(

which is not a part of a JSON string.


In order to get correct data, a fast workaround is ignore this characters AND the final parenthesis:

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

Now you have a JSonObject, so you can refer it by attribute names:

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

OUTPUT (reduced)

{"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!!!)

COMPLETE CODE:

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

}