anonymous
anonymous

Reputation: 1330

Random MalformedJsonException

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!

Upvotes: 0

Views: 2290

Answers (1)

Boris Strandjev
Boris Strandjev

Reputation: 46963

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.

Upvotes: 1

Related Questions