7

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?

user1007522
  • 7,858
  • 17
  • 69
  • 113

3 Answers3

3

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.

user1007522
  • 7,858
  • 17
  • 69
  • 113
  • 3
    I just did `.setClient(new OkClient())`, but yeah OkHttp solved this problem as well as greatly sped up my HTTP requests. Also, you can accept your own answer. – theblang Apr 16 '14 at 13:13
  • I think you should reference `compile 'com.squareup.okhttp:okhttp:x.y.z'` instead of `compile 'com.squareup.okhttp:okhttp-tests:x.y.z`. – JJD Jan 06 '15 at 16:47
  • I'm still getting the same error, so it doesn't seem to have solves this on JellyBean. – Ewald Jan 12 '15 at 15:10
0

This bug seems to happen because of previous connexion reused. You can disable keepalive to avoid it :

System.setProperty("http.keepAlive", "false");
0

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();
Prithiv Dharmaraj
  • 357
  • 2
  • 4
  • 17