Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EOFException in Retrofit while executing synchronous PUT requests

In my app I make synchronous PUT requests using the Retrofit library. The problem is: sometimes the library throws EOFExceptions.

Below is a stack trace for one of such cases

29099-29269/com.mycompany.myapp D/Retrofit﹕ java.io.EOFException   at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:192)   at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:189)   at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:101)   at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:676)   at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:426)   at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:371)   at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:466)   at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)   at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)   at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)   at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)   at $Proxy7.addEvents(Native Method)   at com.mycompany.myapp.api.MyService.addEvents(MyService.java:59) 

I tried the following prosposed solutions but none of them helped in my case:

  • Retrofit gives EOFException only the first time
  • bug retrofit.RetrofitError: java.io.EOFException for Android
  • 1.4.0 New line in String causes retrofit.RetrofitError: java.io.EOFException

Here is how I create RestAdapter in my app:

OkHttpClient okHttpClient = new OkHttpClient();  RestAdapter restAdapter = new RestAdapter.Builder()   .setEndpoint(Url)   .setRequestInterceptor(new RequestInterceptor()   {       @Override       public void intercept(RequestFacade request)       {           request.addHeader("Accept", "application/json");           request.addHeader("Content-Type", "application/json");       }   })   .setClient(new OkClient(okHttpClient))   .setLogLevel(RestAdapter.LogLevel.FULL)   .setErrorHandler(new MyErrorHandler())   .build(); 

Does any one know an other solution for the problem?

Btw, I can't use solutions involving System.setProperty("http.keepAlive", "false"); because I need to keep my connection alive because of performance reasons.

like image 394
Bobrovsky Avatar asked Sep 17 '14 06:09

Bobrovsky


1 Answers

This issue can commonly be caused by a network error. If it is reproducing consistently, consider the potential cause of content length and request timeout. Sometimes timed out network requests can return an improper End of File. Which would throw this error as a result.

like image 76
Codetector Avatar answered Oct 13 '22 19:10

Codetector