JSONSyntaxException

3,408 views
Skip to first unread message

Vivek Jha

unread,
Mar 14, 2014, 9:19:49 AM3/14/14
to googl...@googlegroups.com
 Stacktrace: 
1. com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
2. at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
3. at com.google.gson.Gson.fromJson(Gson.java:803)

I am reading the json from stream that I am getting from my server. The same url when I hit in browser i am getting expected json. I am also passing class in fromjson call.

It occurs about 0.4% of the time and together with the fact that it is happening at line 1 column 1 makes me think that it was some temporary network issue.

can anyone help me on this?
Is it really a network issue?

How do I handle it?

Inderjeet Singh

unread,
Mar 16, 2014, 12:23:53 AM3/16/14
to googl...@googlegroups.com
Can you look at the cause field of the JsonException and see what is going in?

Vivek Jha

unread,
Mar 16, 2014, 1:06:44 AM3/16/14
to googl...@googlegroups.com
Already did.
I have only this in our logging framework.

Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
at .google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)

I am not able to reproduce the issue so can't provide anything more detailed.


Inderjeet Singh

unread,
Mar 18, 2014, 3:39:33 AM3/18/14
to googl...@googlegroups.com
This error typically happens when the JSON is not matching the expected data structure. 
The Java class field is an Object, but the JSON value is a string.

Specifically, in your case the error happened at line 1, column 1. Which means that the server is sending JSON that doesn't start with {

Can you print out the received input and see what is going on?

Inderjeet Singh

unread,
Mar 18, 2014, 3:40:43 AM3/18/14
to googl...@googlegroups.com
This may also happen if the server is returning an error response. Typically, error response is HTML instead of JSON and that may throw Gson off too.

So, in your code, you should make sure that HTTP response code is 200, and not something else.
Reply all
Reply to author
Forward
0 new messages