okhttp: SocketTimeoutException on Retrofit 1.9.0 + OkHttp 2.3.0 + Android 4.4.4
I’m using Retrofit 1.9.0 + OkHttp 2.3.0 + okhttp-urlconnection 2.3.0.
It works well on most android devices, but one Android 4.4.4 KTU840 phone (xiaomi - MI 4LTE).
The SocketTimeoutException
often appear after one or more service calls.
Whenever the exception appeared, no more request can work well, I have to restart the application.
Is it a known bug?
When I remove okhttp-urlconnection
and setup timeout of okhttp
client, no help to solve it.
When I use UrlConnectionClient
(provided by retrofit) instead of OkClient
, it works well.
Sorry for my bad english!
Output of logcat:
07-27 16:49:36.224 30449-31180/com.xxx.xxx D/Retrofit﹕ ---> HTTP GET http://xxx.xxx.xxx.xxx/products/search
07-27 16:49:36.224 30449-31180/com.xxx.xxx D/Retrofit﹕ Accept: application/json
07-27 16:49:36.224 30449-31180/com.xxx.xxx D/Retrofit﹕ User-Agent: XXX/1.1.0.3(beta) (App) Dalvik/1.6.0 (Linux; U; Android 4.4.4; MI 4LTE MIUI/V6.3.6.0.KXECNBL)
07-27 16:49:36.224 30449-31180/com.xxx.xxx D/Retrofit﹕ ---> END HTTP (no body)
07-27 16:49:52.615 30449-31180/com.xxx.xxx D/Retrofit﹕ ---- ERROR http://xxx.xxx.xxx.xxx/products/search
07-27 16:49:52.638 30449-31180/com.xxx.xxx D/Retrofit﹕ java.net.SocketTimeoutException
at java.net.PlainSocketImpl.read(PlainSocketImpl.java:495)
at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
at okio.Okio$2.read(Okio.java:137)
at okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:300)
at okio.RealBufferedSource.indexOf(RealBufferedSource.java:289)
at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:196)
at com.squareup.okhttp.internal.http.HttpConnection.readHeaders(HttpConnection.java:219)
at com.squareup.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:198)
at com.squareup.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:80)
at com.squareup.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:830)
at com.squareup.okhttp.internal.http.HttpEngine.access$200(HttpEngine.java:95)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:823)
at com.squareup.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:684)
at com.squareup.okhttp.Call.getResponse(Call.java:272)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:228)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:199)
at com.squareup.okhttp.Call.execute(Call.java:79)
at retrofit.client.OkClient.execute(OkClient.java:53)
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:326)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
...
at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:242)
at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
07-27 16:49:52.639 30449-31180/com.xxx.xxx D/Retrofit﹕ ---- END ERROR
My code:
public static RestAdapter createRestAdapter(String baseUrl, ApiRequestInterceptor interceptor, Gson gson) {
return new RestAdapter.Builder()
.setConverter(new GsonConverter(gson))
.setEndpoint(baseUrl)
.setClient(createClient())
.setRequestInterceptor(interceptor)
.setProfiler(interceptor)
.setErrorHandler(ApiException.ERROR_HANDLER)
.build();
}
public static Client createClient() {
if (isOkHttpEnabled()) {
if (isOkHttpTimeoutEnabled()) {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(15, TimeUnit.SECONDS);
client.setReadTimeout(15, TimeUnit.SECONDS);
client.setWriteTimeout(15, TimeUnit.SECONDS);
return new OkClient(client);
}
return new OkClient();
} else {
return new UrlConnectionClient();
}
}
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 31 (4 by maintainers)
I solved that problem increasing writeTimeout.
OkHttpClient.Builder builder = new OkHttpClient(). builder.connectTimeout(5, TimeUnit.MINUTES); writeTimeout(5, TimeUnit.MINUTES); readTimeout(5, TimeUnit.MINUTES); okHttpClient = builder.build();
It’s annoying. Those subscribed to these projects don’t need to get 4 emails from you with the same thing.
On Thu, Oct 22, 2015 at 4:19 PM Igor Ganapolsky notifications@github.com wrote:
it’s wired, when i use vpn on android device, it will be connect to the server and can send request and receive response but if i switch off vpn, all requests will be
SocketTimeoutException
. but i still can get request and response from server using postman tool.Same here, issue still present.
Still experience the issue with the following setup in build.gradle: compile ‘com.squareup.okhttp:okhttp:2.5.0’ compile ‘com.squareup.okhttp:okhttp-urlconnection:2.5.0’ compile ‘com.squareup.okio:okio:1.6.0’
android:vmSafeMode=“true” didn’t fix the issue
I’m still experiencing the same issue with Retrofit 2.1.0 and OkHttp 3.3.0
You don’t need to comment on every issue.
On Thu, Oct 22, 2015 at 4:15 PM Igor Ganapolsky notifications@github.com wrote:
I get the same exception. In my case, I turn on the airplane mode and then turn it off, and I get the timeout error.
using okhttp 2.7.0 on android 6.0.0 Nexus 5x x86 Emulator
Update: Just tested it my code on an actual device Motorola Moto X pure edition Marshmallow 6.0.0 and all looks good.