0

I'm developing an Android (2.2) application in which I get from a web service a string containing the JSON serialized version of a list of custom objects, which results in a JSONArray string of JSONObject. The received string is well formed, as I can see at eyesight and as I can prove with online validator. At one point in the Android application, I pass this string to a JSONArray constructor, to get an array of JSONObject. Here's the problem: the JSONArray constructor throws an exception, stating that at some point the string ends with a invalid terminator: but the point indicated in the exception is far from the real end of the string. The fact is that the string in question is huge (about 160000 chars): does anyone know if there's a limit to the length of a string when passed to the JSONArray constructor? The string variable held in memory is full-length, so it seems to me that the JSONArray constructor kindof truncates it. Is it possible?

Thanks in advance, regards

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
e-cal
  • 1,661
  • 2
  • 20
  • 28

2 Answers2

0

Try removing the spaces at the end of the string if any and parse again. Use trim() on string and check once.

sat
  • 40,138
  • 28
  • 93
  • 102
  • Thanks, I'll give it a try next Monday. By the way, I doubt this will solve the problem, because the exception raise when the string parsing is just at the beginning (about 1000/160000 chars). – e-cal May 01 '11 at 09:37
0

Ok guys, it's all my fault. I read the string from an http stream and I did not realize that sometimes I get the string when it's not completed (hence the exception which I tought to be be JSONArray fault).

e-cal
  • 1,661
  • 2
  • 20
  • 28
  • What did you do to fix this? I'm having a similar problem where I use a StringBuilder to create the response String while I can read from the stream. – Blaskovicz Nov 10 '11 at 03:40
  • I don't remember the code well, and I don't have it at hands, but you have to read the FULL stream which contains your data, to have the correct json string. Try to search something about reading an http stream in android, as this: http://stackoverflow.com/questions/2492076/android-reading-from-an-input-stream-efficiently – e-cal Nov 10 '11 at 08:19