java.net.SocketTimeoutException:超时

java.net.SocketTimeoutException:超时,java,android,httpurlconnection,okhttp3,Java,Android,Httpurlconnection,Okhttp3,使用OkHttp库,应用程序面临以下SocketTimeoutException问题。如果请求大小较小,则工作正常(小于1MB)。我在10秒内收到此异常,甚至我的套接字超时(readTimeout)值也要高得多。它始终无法满足请求(大小为1.8MB)。当我使用HttpUrlConnection执行请求时,它工作正常。失败的可能原因是什么 03-29 12:16:38.997 32066-4018/com.mobile W/System.err: java.net.SocketTimeout

使用
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 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+Reformation+Coroutines,则只需使用
try
catch
进行网络操作,例如

viewModelScope.launch(Dispatchers.IO) {
        try {
            val userListResponseModel = apiEndPointsInterface.usersList()
            returnusersList(userListResponseModel)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
其中,异常类型为
kotlin
,而不是
java.lang

这将处理每个异常,如

  • HttpException
  • SocketTimeoutException
  • 致命异常:DefaultDispatcher等
  • 这是我的
    usersList()
    函数

    @GET(AppConstants.APIEndPoints.HOME_CONTENT)
    suspend fun usersList(): UserListResponseModel
    

    检查您的url是否正确。此错误可能是因为错误的url行

    为客户端提供了更大的读取超时值。从套接字读取流时似乎发生超时。@NikolaDespotoski我已将套接字超时设置为15分钟,但我仍面临此问题。我已将连接超时值设置为30秒,将套接字超时值设置为15分钟。但我仍然面临着同样的问题,应用程序在10分钟内就超过了异常seconds@DBragion根据Square提供的文件。如果请求大小超过1MiB,建议不要使用此post字符串方法。您可以回退到HttpUrlConnection方法,或者需要在文档中描述的位置实现后流。@Vivek--您找到解决方案了吗?我遇到了类似的情况。没有实现后流式处理或使用httpurlconnectionI已设置为5分钟,但我仍收到此问题设置也写入解决了问题,谢谢:)如果我们要上载文件,如上所述,我们必须更改默认writeTimeout。这与其他人的评论有什么不同?没有像前两个(setConnectTimeout和setReadTimeout)那样的设置程序了,我认为我的设置程序更有条理:)而且,没有一个标记为已解决;)
    @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()
            }