studio example android httpurlconnection okhttp3

studio - okhttp3 android example



java.net.SocketTimeoutException: tiempo de espera (2)

Para OkHttp 3, el valor predeterminado para OkHttp es 10 segundos. Puede aumentar el tiempo de espera a 30 segundos.

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

Con la biblioteca OkHttp , la aplicación se enfrenta al siguiente problema de SocketTimeoutException . Si el tamaño de la solicitud es menor, entonces está funcionando bien (menos de 1 MB). Recibo esta excepción en 10 segundos, incluso mi valor de tiempo de espera de socket ( readTimeout ) es mucho mayor. Está fallando constantemente para una solicitud (el tamaño es 1.8MB). Cuando HttpUrlConnection una solicitud con HttpUrlConnection está funcionando bien. ¿Cuál podría ser una posible razón de fracaso?

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


writeTimeout() ese problema incrementando writeTimeout() .

Tratar:

OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.connectTimeout(5, TimeUnit.MINUTES) // connect timeout .writeTimeout(5, TimeUnit.MINUTES) // write timeout .readTimeout(5, TimeUnit.MINUTES); // read timeout okHttpClient = builder.build();