0

I sometimes have problems with the Gson().fromJSON function... My app deserialize a JSON string using Gson() and it works almost all the time perfectly, but several times I get this error:

com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Expected literal value at line 1 column 7592
at com.google.gson.Gson.fromJson(Gson.java:769)
at com.google.gson.Gson.fromJson(Gson.java:721)
at com.google.gson.Gson.fromJson(Gson.java:670)
at com.google.gson.Gson.fromJson(Gson.java:642)
at my.new.prog.Main$GetData.onPostExecute(Main.java:301)
at my.new.prog.Main$GetData.onPostExecute(Main.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.google.gson.stream.MalformedJsonException: Expected literal value at line 1 column 7592
at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1298)
at com.google.gson.stream.JsonReader.readLiteral(JsonReader.java:1195)
at com.google.gson.stream.JsonReader.nextValue(JsonReader.java:789)
at com.google.gson.stream.JsonReader.objectValue(JsonReader.java:766)
at com.google.gson.stream.JsonReader.peek(JsonReader.java:380)
at com.google.gson.stream.JsonReader.advance(JsonReader.java:426)
at com.google.gson.stream.JsonReader.skipValue(JsonReader.java:637)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:174)
at com.google.gson.Gson.fromJson(Gson.java:755)
... 16 more

I think that this error appears when the connection fails just a lil bit and it retrieve a corrupted JSON String. I repeat that this error appears randomly and in different parts of the JSON string.

In this case I'm getting the error here: Expected literal value at line 1 column 7592
But sometimes I get the error here: Expected literal value at line 1 column 140

What do you think? Could be that the problem I'm having is caused by a corrupted JSON string?

Thank you in advance!

anonymous
  • 1,320
  • 5
  • 21
  • 37

1 Answers1

1

I think that the problem is caused by interrupted connection. This is totally certain if the exception shows for the same json string. Otherwise there is small possibility that from time to time you generate really malformed json files.

If the problem is with connection being broken, consider retrying until you get rid of this exception. If the file is big, you might find the resumable download thread helpful. However, if the file size is small I wouldn't advise you to go as complex as that.

PS: I never had such inconsistent behavior from GSON, so I think it is not a problem in the library itself, if that is what you think.

Community
  • 1
  • 1
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135
  • Thank you, I also think so. Is there any function to call inside the `catch` to reset what is inside the `try`? – anonymous Apr 10 '12 at 09:27
  • @KrLx_roller what you mean reset? If you mean to the values before you go in the try block, there is no such. I would advise you to place the try in a while and make sure you initialize all the needed variables in the catch case accordingly. – Boris Strandjev Apr 10 '12 at 09:33
  • By resetting I mean doing again what is inside the `try` bloc, I mean, if `catch` catches an error, force to do do what is inside the `try` bloc. This way, if `catch` catches an error, it will be getting the data again and again until the data is correct. So, is there any way to do this? – anonymous Apr 10 '12 at 09:37
  • Just place the try/catch inside a while with a boolean flag, it should be sufficient for you – Boris Strandjev Apr 10 '12 at 09:39
  • I'll implement your idea by doing what I found here. Thank you! http://stackoverflow.com/questions/9905445/try-catch-doing-something-if-the-try-completed-successfully – anonymous Apr 10 '12 at 10:31