Hot questions for Handling EOFException in Retrofit
Retrofit gives EOFException only the first time
Question: I'm using framework Retrofit for the first time in my Android project. It handles communication with a backend. Now the strangest part is that on Android 4.4 everything works like a charm. On every version below. I get a RetrofitError type java.io.EOFException. So it fails the first time and then when I push on the retry button it works. So Why is it failing the first time?
I want to fix this because it is annoying that users needs to click retry.
Does someone got a solution for this?
Answer: In Android 4.4 they work with OkHttpclient so thats the reason why it is working on 4.4 and not on the older Android versions.
To solve this add a dependency in gradle:
and create a new client like this:
OkHttpClient client = new OkHttpClient();
add that new client to the restadapter to use this:
The error should be solved now.
bug retrofit.RetrofitError: java.io.EOFException for Android
Question: I search a lot, I found some solutions like this:
System.setProperty("http.keepAlive", "false"); arg0.addHeader("Connection", "close"); arg0.addHeader("Accept-Language", "en-US,en;q=0.8");
I try them but didn't work for me. How can I correct this bug? Here is my log:
D/Retrofit(28969): ---> HTTP POST http://release.api.jeunesse.build.indus.ftven.net/app.php/api/users D/Retrofit(28969): Connection: close D/Retrofit(28969): Content-Type: application/json D/Retrofit(28969): Content-Length: 169 ...
Answer: the solution was to use both OkClient and OkHttp. After adding these two libraries I set the client on Retrofit to OkHttp like that:
RestAdapter restAdapter = new RestAdapter.Builder() .setErrorHandler(new ErrorRetrofitHandlerException()) .setEndpoint(urlBase) .setLogLevel(RestAdapter.LogLevel.FULL) .setClient(new OkClient(new OkHttpClient())) .build();