gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:05:35 30 4
gpt4 key购买 nike

我正在尝试从 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();

异常(exception):

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();

关于android - 仅在某些 Android 设备上抛出 java.net.SocketTimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47504326/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com