Bobrovsky
Bobrovsky

Reputation: 14246

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:

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.

Upvotes: 27

Views: 2653

Answers (3)

Codetector
Codetector

Reputation: 774

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.

Upvotes: 2

Joan Casadellà
Joan Casadellà

Reputation: 286

You can look at this answer with similar problem.

If you are with big requests (i think this is more than 50Mb of data) and you are not using pagination system in the server side, you can get this type of error.

Also it can be a TimeOut of the server side, for example.

Upvotes: 0

TimJowers2
TimJowers2

Reputation: 228

Perhaps EOF as lost network connection or timed out. Try increasing your timeout and ensuring keep-alive is set.

Upvotes: 0

Related Questions