Android Retrofit GET Request ConversionException

I am using Retrofit to make REST requests and create the corresponding model objects (using gson using @SerializedName annotation). There is one particular GET request that sometimes throws a ConversionException and I am having trouble tracking down the cause. I'll make 10-20 similar GET requests almost simultaneously with different parameters, and about 90% of them will return correctly. Others fail with ConversionException:

retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: BEGIN_OBJECT expected but STRING at line 1 column 1

Once this conversion exception has happened, whenever I try to repeat this request (without killing the application) it will always have the same conversion exception. If I kill the app and start completely fresh, it probably works.

I tried to view the json response in the browser for requests that had this conversion exception, but it always looks correct. I've done a lot of comparisons between "failed" requests and successful ones and the json looks the same.

My questions:

1) Why do these conversion exceptions happen when, as far as I can tell, the answer data is always correct?

2) Does any caching upgrade for GET requests? This might explain why re-requesting a failed request keeps failing until I kill and restart the application.

Thank!

0


source to share


1 answer


1) Why do these conversion exceptions happen when, as far as I can tell, the answer data is always correct?

The expected data looks like this:

{"foo":"bar"}

      

But Gson found something more:

Hello!

      



It was expected to be the start of a JSON object (aka a character {

), but it will find a string character instead.

2) Does any caching upgrade for GET requests? This might explain why re-requesting a failed request keeps failing until I kill and restart the application.

Retrofit has no caching at all.

Depending on which HTTP client you are using, it may cache the request response GET

depending on the headers. Usually you should opt out of the cache behavior in the HTTP client, so if you haven't, I doubt it is enabled.

+1


source







All Articles