Question

I'm using Retrofit to make REST requests and create the corresponding model objects (with gson using the @SerializedName annotation). There is one particular GET request that occasionally causes a ConversionException though, and I am having trouble tracking down the cause. I will make 10-20 similar GET requests nearly at the same time with varying parameters and about 90% of them return correctly. The remaining fail with a ConversionException:

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

Once this conversion exception has occurred, any time I try to make that request again (without killing the app) it will always have the same conversion exception. If I kill the app and start completely fresh, it will probably work.

I've tried looking at the json response in the browser for requests that had this conversion exception, but it always looks correct. I've done many comparisons between "failed" requests and successful ones and the json looks identical.

My questions:

1) Why are these conversion exceptions occurring, when as far as I can tell, the response data is always correct?

2) Does retrofit do any sort of caching for GET requests? This might explain why re-requesting a failed request continues to fail until I kill and restart the application.

Thanks!

Was it helpful?

Solution

1) Why are these conversion exceptions occurring, when as far as I can tell, the response data is always correct?

You expected data that looks like this:

{"foo":"bar"}

But Gson found something more like:

Hello!

It was expecting a JSON object beginning (aka the { character) but it found a string-like character instead.

2) Does retrofit do any sort of caching for GET requests? This might explain why re-requesting a failed request continues to fail until I kill and restart the application.

Retrofit has no caching whatsoever.

Depending on what HTTP client you are using, it may cache the response of a GET request depending on the headers. Usually you have to opt-in to cache behavior in an HTTP client so if you haven't done that, I doubt it's enabled.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top