4

I'm woking on an Android app which is calling a REST WS. The service itself works fine (tested with Postman) but I keep getting occasionally this exception from Android:

: java.io.IOException: unexpected end of stream on com.squareup.okhttp.Address@2cd22e9d
                                             at com.squareup.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:201)
                                             at com.squareup.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127)
                                             at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737)
                                             at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:87)
                                             at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:722)
                                             at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:576)
                                             at com.squareup.okhttp.Call.getResponse(Call.java:287)
                                             at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
                                             at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
                                             at com.squareup.okhttp.Call.execute(Call.java:80)
                                             at retrofit.client.OkClient.execute(OkClient.java:53)
                                             at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
                                             at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
                                             at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
                                             at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                             at retrofit.Platform$Android$2$1.run(Platform.java:142)
                                             at java.lang.Thread.run(Thread.java:818)
                                          Caused by: java.io.EOFException: \n not found: size=0 content=...
                                             at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:201)
                                             at com.squareup.okhttp.internal.http.Http1xStream.readResponse(Http1xStream.java:186)
                                             at com.squareup.okhttp.internal.http.Http1xStream.readResponseHeaders(Http1xStream.java:127) 
                                             at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:737) 
                                             at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:87) 
                                             at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:722) 
                                             at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:576) 
                                             at com.squareup.okhttp.Call.getResponse(Call.java:287) 
                                             at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243) 
                                             at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205) 
                                             at com.squareup.okhttp.Call.execute(Call.java:80) 
                                             at retrofit.client.OkClient.execute(OkClient.java:53) 
                                             at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326) 
                                             at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220) 
                                             at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278) 
                                             at retrofit.CallbackRunnable.run(CallbackRunnable.java:42) 
                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                             at retrofit.Platform$Android$2$1.run(Platform.java:142) 
                                             at java.lang.Thread.run(Thread.java:818) 

I've find something online about a bug, but I'm not sure. Also, the strange thing is that sometimes works and sometimes don't, without me changing anything in the code. The exception says something about a missing end line character. Do I have to put it in the URL? Here's some code. Thank you.

Api interface

public interface ApiInterface {

@Headers({"Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJkb25hdG8iLCJ1c2VySWQiOiIxIiwicm9sZSI6IlJPTEVfQURNSU4ifQ.yNVtQMfMasfR7nfBgtjtmlXMM8Gpfjvf_FDz1sMiaSJxUvLDyC9sjm-XSEkJdfGy4JasfweRrRUGc3gHpMRA",
            "Accept: application/json",
"Content-Type: application/json;charset=utf-8"})
@POST("/todo/getTodoList")
public void getTodoList(@Body BaseRequest request, Callback<GetTodoListResponse> response);
}

Controller

 RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("http://www.myUrl.com/app/rest").setLogLevel(RestAdapter.LogLevel.FULL).setConverter(new GsonConverter(builder.create()))
            .setClient(new OkClient(new OkHttpClient()))
            .build();
    ApiInterface methods = restAdapter.create(ApiInterface.class);
    Callback<GetTodoListResponse> callback = new Callback<GetTodoListResponse>() {
        @Override
        public void success(GetTodoListResponse o, Response response) {
            todoList = o.getTodoList();
            Log.d("CardController", "TodoList: " + todoList);
            if (!o.isOutcome()) {
                Log.e("CardController", "Error fetching todoList");
                todoList = new ArrayList<Todo>();
            } else {
                setChanged();
             myMethod();
            }
        }

        @Override
        public void failure(RetrofitError retrofitError) {
            Log.e("Retrofit", "Error fetching todoList: ", retrofitError);
        }
    };
    methods.getTodoList(new AuraBaseRequest(), callback);
Aurasphere
  • 3,841
  • 12
  • 44
  • 71
  • 1
    It looks to be same as of http://stackoverflow.com/questions/24806797/bug-retrofit-retrofiterror-java-io-eofexception-for-android – Gaurav Jul 11 '16 at 22:51
  • @Gaurav thank you for the comment but, as you can see, in my code I'm already using the OkClient as stated in that answer and I get the problem anyway. The thing I don't get is why this error is inconsistent... I'm still looking for a solution after 2 months... – Aurasphere Jul 23 '16 at 17:25
  • @Aurasphere Were you able to solve this problem? i'm stuck on this issue from 2 days – rathodbhavikk Jun 27 '18 at 13:29
  • @BhavikRathod good luck, I've been stuck on this issue for 2 years and still I have no clue on how to solve it :( – Aurasphere Jun 27 '18 at 14:23
  • 1
    @Aurasphere So this is the only solution, i have to retry API call if i receive this issue. and one more thing to confirm that are you using more than one Retrofit Library with different version in single project – rathodbhavikk Jun 27 '18 at 14:30
  • @BhavikRathod I'm sorry I can't confirm that because I don't work anymore on this project and I honestly can't remember for sure but I don't think I had more than one version of it. – Aurasphere Jun 27 '18 at 15:01

1 Answers1

1

In my case, the server side was Spring MVC, and I was getting this error. This error happened due to some security check (500 return status), but error in retrofit was unexpected end of stream. Once the security check was bypassed, I was able to get the response.

Hope it might help someone in future.

Monster Brain
  • 1,950
  • 18
  • 28