2016-04-06 39 views
14

Con la libreria OkHttp, l'applicazione sta affrontando il problema SocketTimeoutException. Se la dimensione della richiesta è inferiore, allora funziona bene (meno di 1 MB). Ricevo questa eccezione entro 10 secondi, anche il valore di timeout del socket (readTimeout) è molto più alto. Sta costantemente fallendo una richiesta (la dimensione è 1.8MB). Quando ho eseguito una richiesta con HttpUrlConnection, funziona correttamente. Quale potrebbe essere una possibile causa di fallimento?java.net.SocketTimeoutException: timeout

03-29 12:16:38.997 32066-4018/com.mobile W/System.err: java.net.SocketTimeoutException: timeout 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okio.Okio$3.newTimeoutException(Okio.java:207) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okio.AsyncTimeout.exit(AsyncTimeout.java:261) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okio.AsyncTimeout$1.write(AsyncTimeout.java:158) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okio.RealBufferedSink.write(RealBufferedSink.java:46) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okhttp3.internal.http.Http1xStream$FixedLengthSink.write(Http1xStream.java:286) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:176) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okio.RealBufferedSink.write(RealBufferedSink.java:96) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okhttp3.RequestBody$2.writeTo(RequestBody.java:96) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:704) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:563) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okhttp3.RealCall.getResponse(RealCall.java:241) 
    03-29 12:16:38.997 32066-4018/com.mobile W/System.err:  at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at okhttp3.RealCall.execute(RealCall.java:57) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at com.mobizio.api.BaseApi.sendOkHttpRequest(BaseApi.java:81) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at com.mobizio.api.BaseApi.doInBackground(BaseApi.java:45) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at com.mobizio.api.BaseApi.doInBackground(BaseApi.java:30) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at android.os.AsyncTask$2.call(AsyncTask.java:292) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at java.lang.Thread.run(Thread.java:818) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err: Caused by: java.net.SocketException: socket is closed 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:759) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at okio.Okio$1.write(Okio.java:80) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err:  at okio.AsyncTimeout$1.write(AsyncTimeout.java:155) 
    03-29 12:16:38.998 32066-4018/com.mobile W/System.err: ... 20 more 
+0

Lascia la tua cliente un tempo di lettura più grande valore di timeout. Sembra che il timeout stia accadendo quando lo stream viene letto dal socket. –

+0

@NikolaDespotoski Ho già impostato il timeout del socket su 15 minuti, ma sto affrontando questo problema – Vivek

risposta

22

Per OkHttp 3 il valore predefinito per OkHttp è 10 secondi. È possibile aumentare il timeout a 30 secondi.

OkHttpClient client = new OkHttpClient(); 
client.setConnectTimeout(30, TimeUnit.SECONDS); // connect timeout 
client.setReadTimeout(30, TimeUnit.SECONDS); // socket timeout 
+0

Ho già impostato il valore di timeout della connessione su 30 secondi e il valore di timeout del socket su 15 minuti. Sto ancora affrontando lo stesso problema e l'app sta passando sopra l'eccezione entro 10 secondi – Vivek

+0

@Vivek hai trovato una soluzione? – DBragion

+1

@DBragion Come da documentazione fornita da Square https://github.com/square/okhttp/wiki/Recipes. Si consiglia di non utilizzare questo metodo di stringa post se la dimensione della richiesta è superiore a 1MiB. È possibile eseguire il fallback al metodo HttpUrlConnection o implementare lo streaming post come descritto nel documento. – Vivek

15

Ho risolto il problema aumentando writeTimeout().

Prova:

OkHttpClient.Builder builder = new OkHttpClient.Builder(); 
builder.connectTimeout(5, TimeUnit.MINUTES) 
.writeTimeout(5, TimeUnit.MINUTES) 
.readTimeout(5, TimeUnit.MINUTES); 

okHttpClient = builder.build();