3

I have an Android application that makes http requests to a REST API in Flask. I am using Retrofit2 with okhttp3 to make requests to a server hosted on a Raspberry Pi with Raspbian Lite. My problem is that sometimes I get an IOException -> java.net.ProtocolException: unexpected end of stream But it happens sometimes, other times it works perfectly. The http client is builded as follows:

OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
        @NotNull
        @Override
        public Response intercept(@NotNull Chain chain) throws IOException {
            Request original = chain.request();

            Request request = original.newBuilder()
                    .header("User-Agent","myUserAgent")
                    .header("Connection","close")
                    .addHeader("Accept-Encoding", "identity")
                    .method(original.method(),original.body())
                    .build();

            return chain.proceed(request);
        }
    });
Retrofit retrofit=null;
    try {
         retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .client(httpClient.build())
                .build();

    }catch (Exception e){
         //Log.d

    }
ApiService API_SERVICE=null;
    try{
        API_SERVICE = retrofit.create(ApiService.class);
    }catch (Exception e){
        //Log.d
    }

    return API_SERVICE;

I have tried with and without logging interceptors, with Accept-Encoding: identity and Conecction: close. But it doesn't work. The Content-Lenght of the answer is 4339 (it is indicated by postman) and in the intercetor it also indicates 4339 This is the exception I get:

enter image description here

I am using Android Studio with an emulator in Android API 28. Both my pc and raspberry are connected by ethernet cable to the Internet. What I don't understand is why sometimes the request works and other times it goes straight to onFailure. On the server, I always get a 200 code. The request is processed and returns a response correctly. what else can i try?

5 Answers5

6

It seems that the problem is with the Android Studio emulator. I have tried connecting my smartphone to android studio and installing the APK on another smartphone and the problem does not reproduce.

2

I tried [this][https://stackoverflow.com/questions/5528850/how-do-you-connect-localhost-in-the-android-emulator/59217370#59217370] and it worked! (My Problem was with localhost IP 192.168.x.x)

Go to the emulator's settings -> Settings - >Proxy -> Select Manual proxy Configurations -> type down your local IP address.

Behnawwm
  • 121
  • 2
  • 9
1

Looks like this issue: https://github.com/square/okhttp/issues/2738. So Try this:

Either:

.addHeader("Connection", "close")

Or: .retryOnConnectionFailure(true)

Dmitri
  • 2,563
  • 1
  • 22
  • 30
  • I already had "Conecction, close". With retryOnConnectFailure is the same. I have now OkHttpClient.Builder httpClient = new OkHttpClient.Builder().retryOnConnectionFailure(true); – Botellita Taponzito May 02 '20 at 18:47
  • strange. based on your StackTrace it fit perfectly. I still feel it is the same issue but additionally something else is going on in your case. – Dmitri May 02 '20 at 19:50
  • also, check this out - https://stackoverflow.com/questions/61422009/retrofit-post-java-io-ioexception-unexpected-end-of-stream-on-connection-caused meaning could it be a server problem? – Dmitri May 02 '20 at 19:58
  • my problem is in okHttp's Http1Codec $ FixedLengthSource.read () method. The server didn't supply the promised content length, but why? The server gives me the same response when the request works as when it doesn't. https://www.codota.com/code/java/classes/okhttp3.internal.http1.Http1Codec$FixedLengthSource – Botellita Taponzito May 03 '20 at 10:21
  • maybe you should consider reopening this issue which was closed because it was not reproducible - https://github.com/square/okhttp/issues/3589 – Dmitri May 03 '20 at 18:35
1

I am experiencing this problem too, I found it only happen when I call simultaneously 2 functions (function A and B) that have the same URL with different params. After function A get success response, sometimes function B received error java.net.ProtocolException: unexpected end of stream

So my work around is call function B after function A get the success response

aiwiguna
  • 2,852
  • 2
  • 15
  • 26
0

This error is also given by retrofit occasionally. When there is a 504 Gateway Time-out problem occurring on the server side. So there is no problem on android side. This answer might help someone.

Rohaitas Tanoli
  • 624
  • 4
  • 16