java.net.SocketTimeoutException仅在某些Android设备上抛出

时间:2017-11-27 05:13:55

标签: android okhttp3

我正在尝试从Android设备上传JPEG图像文件。我正在使用square/okhttp库来创建请求。我在联想瑜伽平板电脑上面临这个问题,当我试图上传图像时,它会得到以下异常。但是当我在三星Galaxy Tab 10上运行相同的代码时,一切正常,图像上传成功。

相机捕获图像并将其存储在/ storage / emulated / 0 /,应用程序将从该处获取图像并尝试上传图像。我有一个在后台运行的服务,用于上传。

final MediaType MEDIA_TYPE_IMAGE_JPEG = MediaType.parse("image/jpeg");

        File file = new File(path);
        Request request = new Request.Builder()
                .url(baseUrl)
                .addHeader("timestamp", map.get("timestamp"))
                .addHeader("Content-Type", map.get("Content-Type"))
                .post(RequestBody.create(MEDIA_TYPE_IMAGE_JPEG, file))
                .build();

        OkHttpClient client = new OkHttpClient();
        Response response = client.newCall(request).execute();

例外:

java.net.SocketTimeoutException: timeout
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err: at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2
Stream.java:593)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err: at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.java:601)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err: at okhttp3.internal.http2.Http2Stream.takeResponseHeaders(Http2Stream.java:146)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err: at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:125)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:88)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
11-27 15:37:06.394 1770-2339/commyapp.app.debug 
W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
11-27 15:37:06.394 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
 11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
 11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
 11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
  W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
 W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
 W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
  11-27 15:37:06.395 1770-2339/com.myapp.app.debug 
  W/System.err:     at okhttp3.RealCall.execute(RealCall.java:77)

1 个答案:

答案 0 :(得分:1)

您必须增加服务器端的读/写超时。 Android的临时解决方法是增加OkHttp3客户端的默认超时:

OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(120, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .build();