Reputation: 121
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:
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?
Upvotes: 3
Views: 13245
Reputation: 121
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.
Upvotes: 2
Reputation: 807
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.
Upvotes: 0
Reputation: 2922
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
Upvotes: 1
Reputation: 121
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.
Upvotes: 6
Reputation: 2982
Looks like this issue: https://github.com/square/okhttp/issues/2738. So Try this:
Either:
.addHeader("Connection", "close")
Or: .retryOnConnectionFailure(true)
Upvotes: 1