Retrofit gives EOFException only the first time
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?
I found a solution. 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:
compile 'com.squareup.okhttp:okhttp-tests:1.5.1'
and create a new client like this:
OkHttpClient client = new OkHttpClient();
add that new client to the restadapter to use this:
setClient(new OkClient(client))
The error should be solved now.
This bug seems to happen because of previous connexion reused. You can disable keepalive to avoid it :
System.setProperty("http.keepAlive", "false");
I finally resolve the problem.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("Yout base URL")
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(new OkClient(new OkHttpClient())) //Http Client
.build();
链接地址: http://www.djcxy.com/p/78026.html