java.net.SocketTimeoutException: timeout

100,311

Solution 1

For OkHttp 3 the default value for OkHttp is 10 seconds. You can increase the timeout to 30 seconds.

OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(30, TimeUnit.SECONDS); // connect timeout
client.setReadTimeout(30, TimeUnit.SECONDS);    // socket timeout

Solution 2

I solved that problem increasing writeTimeout().

Try:

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

Solution 3

this resolved my problem:

OkHttpClient innerClient = new OkHttpClient.Builder()
            .connectTimeout(5, TimeUnit.MINUTES) // connect timeout
            .writeTimeout(5, TimeUnit.MINUTES) // write timeout
            .readTimeout(5, TimeUnit.MINUTES) // read timeout
            .build();

Solution 4

You need to understand that only adding this won't solve your problem:

OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)

If you are using Kotlin + Retrofit + Coroutines then just use try and catch for network operations like,

viewModelScope.launch(Dispatchers.IO) {
        try {
            val userListResponseModel = apiEndPointsInterface.usersList()
            returnusersList(userListResponseModel)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

Where, Exception is type of kotlin and not of java.lang

This will handle every exception like,

  1. HttpException
  2. SocketTimeoutException
  3. FATAL EXCEPTION: DefaultDispatcher etc

Here is my usersList() function

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

Solution 5

Use this for 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()
Share:
100,311

Related videos on Youtube

Vivek
Author by

Vivek

Updated on July 08, 2022

Comments

  • Vivek
    Vivek almost 2 years

    With OkHttp library, application is facing following SocketTimeoutException issue. If request size is less, then it is working fine(Less than 1MB). I am getting this exception within 10 seconds, even my socket timeout(readTimeout) value is much higher. It is consistently failing for a request(Size is 1.8MB). When I executed a request with HttpUrlConnection it is working fine. What could be a possible reason of failure?

       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
    
    • Nikola Despotoski
      Nikola Despotoski about 8 years
      Give your client a larger read time out value. Seems that time out is happening when the stream is read from the socket.
    • Vivek
      Vivek about 8 years
      @NikolaDespotoski I already set Socket timeout to 15 minutes still I am facing this issue
  • Vivek
    Vivek about 8 years
    I already set the connection timeout value to 30 seconds and socket timeout value to 15 minutes. Still I am facing the same issue and app is getting above exception within 10 seconds
  • DBragion
    DBragion almost 8 years
    @Vivek did you find a solution?
  • Vivek
    Vivek almost 8 years
    @DBragion As per documentation provided by Square github.com/square/okhttp/wiki/Recipes. It is suggested that don't use this post string method if request size is more that 1MiB. You can fallback to HttpUrlConnection method or you need to implement post streaming at described in the document.
  • Alyoshak
    Alyoshak over 7 years
    @Vivek -- did you ever find a solution? I'm experiencing something similar.
  • Vivek
    Vivek over 7 years
    No implemente post streaming or use httpurlconnection
  • Bhavesh Hirpara
    Bhavesh Hirpara over 6 years
    I have set to 5 min Still Im getting this issue
  • Yusuf Çağlar
    Yusuf Çağlar about 6 years
    Setting also writeTimeout solved the problem, thanks :)
  • Nandha Kumar
    Nandha Kumar over 5 years
    If we want to upload file, we have to change the default writeTimeout as mentioned above.
  • Zun
    Zun almost 5 years
    How is this any different than what others have commented?
  • SBen
    SBen almost 5 years
    There are no more setters like the first two (setConnectTimeout & setReadTimeout), and I think mine is more organized :) Also, none is marked as resolved ;)
  • 4xMafole
    4xMafole almost 3 years
    Saved my day. I was about to mess around on the internet
  • Kishan Solanki
    Kishan Solanki almost 3 years
    What if server down? Then it behaves same like wrong URL as response won't be arriving from server. So setting proper URL is not the solution.
  • IonicMan
    IonicMan over 2 years
    I think its the prettiest solution so far
  • gulab patel
    gulab patel over 2 years
    This one helped me but can you please tell what is default time set for timeout? and is there any other clean method to avoid this problem?