简体   繁体   中英

Android HttpUrlConnection InputStream reading error(JSONException: Unterminated array)

I am using an HttpUrlConnection to GET a very large JSON array from the web. I am reading the data 500 bytes at a time as so:

 public String getJSON(String myurl) throws IOException {

    URL url = new URL(myurl);

    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try {
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        String result = readIt(in, 500) ;

        return result ;
        //Log.d(TAG, result);
    }
    finally {
        urlConnection.disconnect();
    }
}

public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    InputStreamReader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[len];

    while(reader.read(buffer) != -1)
    {
        System.out.println("!@#: " + new String(buffer)) ;
        result.append(new String(buffer)) ;
        buffer = new char[len];
    }
    System.out.println(result.length()) ;
    return result.toString();
}

This works fine on some phones, but not on newer phones. On newer phones I realized that the result JSON string was starting to contain garbage characters once it got to character 2048.

Some of my garbage return data:
ST AUGUSTINE, FL","2012050

And the full error is: Error: org.json.JSONException: Unterminated array at character 40549 of {"COLUMNS":["IMAGELI

Probably you append a wrong buffer to your string. You should count the number of char you get when reading and append them to the string but no more:

String str = new String(); // or use a StringBuilder if you prefer
char[] buffer = new char[len];

while ((count = reader.read(buffer, 0, len)) > 0) 
{ str += new String(buffer, 0, count); }

Avoid recreate your buffer each time ! You allocate a new one for each loop... Reuse the buffer, as you have flushed it in str.

Be carreful when debugging: you cannot print a too long string in logcat (it will be cut if too long). But your str should be fine and should not contain any garbage data anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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