我正在尝试从Android设备上载JPEG图像文件。我正在使用square/okhttp库创建请求。我在Lenovo Yoga平板电脑上遇到此问题,当我尝试上传图像时,出现以下异常。但是,当我在Samsung 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)

最佳答案

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

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

07-26 05:08