2016-04-06 15 views
14

Với thư viện OkHttp, ứng dụng đang gặp phải sự cố SocketTimeoutException sau đây. Nếu kích thước yêu cầu nhỏ hơn, thì nó hoạt động tốt (ít hơn 1MB). Tôi nhận được ngoại lệ này trong vòng 10 giây, ngay cả giá trị thời gian chờ socket (readTimeout) của tôi cao hơn nhiều. Nó liên tục thất bại cho một yêu cầu (Kích thước là 1,8MB). Khi tôi thực hiện một yêu cầu với HttpUrlConnection, nó hoạt động tốt. Điều gì có thể là một lý do có thể có của thất bại?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

Cho khách hàng của bạn một thời gian đọc lớn ra giá trị. Dường như thời gian chờ xảy ra khi luồng được đọc từ ổ cắm. –

+0

@NikolaDespotoski Tôi đã đặt thời gian chờ của Ổ cắm là 15 phút nhưng tôi đang đối mặt với vấn đề này – Vivek

Trả lời

22

Đối với OkHttp 3 giá trị mặc định cho OkHttp là 10 giây. Bạn có thể tăng thời gian chờ lên 30 giây.

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

Tôi đã đặt giá trị thời gian chờ kết nối là 30 giây và giá trị thời gian chờ của ổ cắm là 15 phút. Tuy nhiên tôi đang đối mặt với cùng một vấn đề và ứng dụng đang nhận được trên ngoại lệ trong vòng 10 giây – Vivek

+0

@Vivek bạn đã tìm thấy một giải pháp? – DBragion

+1

@DBragion Theo tài liệu được cung cấp bởi Square https://github.com/square/okhttp/wiki/Recipes. Chúng tôi khuyên bạn không nên sử dụng phương thức chuỗi bài đăng này nếu kích thước yêu cầu cao hơn 1MiB. Bạn có thể dự phòng phương thức HttpUrlConnection hoặc bạn cần triển khai bài đăng trực tuyến tại được mô tả trong tài liệu. – Vivek

15

Tôi đã giải quyết sự cố đó tăng writeTimeout().

Hãy thử:

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

okHttpClient = builder.build();