I've an Android app which connects to a .NET HTTP service on a PC and allows browsing the media directories on that PC. The interface is similar to a file explorer with a list of current directories / media files.
When a user selects a directory name from the list, the contents of that directory are returned in a JSON response.
My problem is sometimes the Android end of things thinks the JSON is incomplete but I don't know why as I've logged the JSON string at the server end and it is correctly terminated. The Android code that handles the response is as follows...
int contentLength = -1;
int totalRead = 0;
int numRead = -1;
InputStream inStream = connHelper.getInputStream();
contentLength = connHelper.getContentLength();
byte[] buffer = new byte[contentLength != -1 ? contentLength : 8192];
while ((numRead = inStream.read(buffer)) != -1)
if (numRead != 0)
totalRead += numRead;
Log.d(TAG, "getMediaList() - totalRead: " + totalRead);
String jsonString = new String(buffer, "UTF-8");
Log.d(TAG, "jsonString.length() - " + jsonString.length());
Log.d(TAG, "jsonString - " + jsonString);
Here's a log from the Android logcat of a good sequence of events...
requestValue: D:\Jukebox\Classical\Classic FM - Smooth Classics (Do Not Disturb) (Disc 2)\
getMediaList() - totalRead: 501
jsonString.length() - 501
jsonString - {"folders":[],"media":[<cut for brevity>]}
...but here's a log of a bad one which is consistent for this list of media...
requestValue: D:\Jukebox\Classical\De Lucia, Paco\Collection\
getMediaList() - totalRead: 353
jsonString.length() - 351
jsonString - {"folders":[],"media":[<cut for brevity>,"14 Concierto de Aranjuez (Adagio).mp3"
Notice the total bytes read for the problem media list is 353 but the jsonString
is only 351 bytes. Notice also that the log of jsonString
shows the closing array/object characters ]}
are missing.
I'm totally confused by this. The .NET server logs show a complete JSON string, the Android logs show 353 bytes are read but the jsonString
length shows only 351 bytes. Can anyone explain what might cause this?