![](/img/trans.png)
[英]InfluxDBIOException: java.net.SocketTimeoutException: timeout
[英]java.net.SocketTimeoutException: timeout
使用OkHttp
庫,應用程序面臨以下SocketTimeoutException
問題。 如果請求大小更小,那么它工作正常(小於 1MB)。 我在 10 秒內得到了這個異常,即使我的套接字超時( readTimeout
)值要高得多。 請求始終失敗(大小為 1.8MB)。 當我使用HttpUrlConnection
執行請求時,它工作正常。 失敗的可能原因是什么?
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
對於 OkHttp 3,OkHttp 的默認值為 10 秒。 您可以將超時增加到 30 秒。
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(30, TimeUnit.SECONDS); // connect timeout
client.setReadTimeout(30, TimeUnit.SECONDS); // socket timeout
我解決了增加writeTimeout()
問題。
嘗試:
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();
這解決了我的問題:
OkHttpClient innerClient = new OkHttpClient.Builder()
.connectTimeout(5, TimeUnit.MINUTES) // connect timeout
.writeTimeout(5, TimeUnit.MINUTES) // write timeout
.readTimeout(5, TimeUnit.MINUTES) // read timeout
.build();
將此用於 Kotlin
val client1 = OkHttpClient.Builder()
.connectTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES) // write timeout
.readTimeout(2, TimeUnit.MINUTES) // read timeout
.addInterceptor(
BasicAuthInterceptor(
AmvaccAppConstants.AUTHENTICATE_USER_NAME, AmvaccAppConstants.AUTHENTICATE_USER_PASSWORD
)
)
.addInterceptor(interceptor)
.build()
您需要了解僅添加此內容並不能解決您的問題:
OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
如果您使用的是Kotlin + Retrofit + Coroutines,那么只需使用try
和catch
進行網絡操作,例如,
viewModelScope.launch(Dispatchers.IO) {
try {
val userListResponseModel = apiEndPointsInterface.usersList()
returnusersList(userListResponseModel)
} catch (e: Exception) {
e.printStackTrace()
}
}
其中,Exception 是kotlin
類型,而不是java.lang
這將處理每個異常,例如,
這是我的usersList()
函數
@GET(AppConstants.APIEndPoints.HOME_CONTENT)
suspend fun usersList(): UserListResponseModel
If you are using Retrofit and Kotlin then use following code:
var BASE_URL:String="Your URL"
val clientSetup = OkHttpClient.Builder()
.connectTimeout(1, TimeUnit.MINUTES)
.writeTimeout(1, TimeUnit.MINUTES) // write timeout
.readTimeout(1, TimeUnit.MINUTES) // read timeout
.build()
val getClientApi: ApiInterface
get() {
var retrofit: Retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(clientSetup)
.build()
}
檢查您的網址是否正確。 此錯誤可能是由於錯誤的 url 行
跑進追趕
runCatching {
service
}
.map {
if (it.isSuccessful && it.body() != null) {
Success(Unit)
} else {
Error(Failure.AuthError.error(it.errorBody()))
}
}
.getOrElse {
Error(Failure.Throwable(it))
}
在我的情況下,將ping 間隔添加到 OkHttp 有助於減少應用程序中的SocketTimeoutExceptions數量。
OkHttpClient.Builder()
.pingInterval(3, SECONDS)
.build()
但我不確定這些超時是否與請求大小有關。 我在 github 上的這個 issue 中也提到了它,報告給 OkHttp 庫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.