I’m having an issue trying to build a JSONArray in android that has more than 89 items in it. It works fine with 89 items but once i put 90 or more in, i get the error “jsonexception expected : after”. I’m still pretty new with the android and java stuff so if theres more error detail i can find that would help, let me know how to do so. I don’t think the issue is with the JSON itself, because when i did verify it is valid json from the url itself. I’ll post the code below.
HttpGet request = new HttpGet(SERVICE_URL + "/GetResListNoStatus/" + FacID);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
//this line here is where the error is occuring
JSONArray plates = new JSONArray(new String(buffer));
If anyone has any ideas i would really appreciate any help anyone can give. Thanks.
You’re only calling read() once on your InputStream. That will read whatever data is available, which may be all of the request or just a few bytes. You’ll need to call read(buffer) repeatedly, appending what was read to a fixed buffer (you could just just write it into a ByteArrayOutputStream), and stop once read() returns -1.
Edit
Try something like this.