Retrofit gives EOFException only the first time

别来无恙 提交于 2019-12-23 08:49:33

问题


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?


回答1:


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.




回答2:


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

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



回答3:


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();


来源:https://stackoverflow.com/questions/22351400/retrofit-gives-eofexception-only-the-first-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!