2

I have a class where I prepare my OkHttpClient and add an Interceptor to it for my Retrofit requests in Android Studio. There it assigns default parameters in all requests.

It works fine, except when I need to make requests with a high timeOut (a few minutes). When that happens, it returns the exception to me after a few seconds:

I/okhttp.OkHttpClient: <-- HTTP FAILED: java.net.SocketTimeoutException: timeout
W/System.err: java.net.SocketTimeoutException: timeout
...
W/System.err: Caused by: java.net.SocketException: socket is closed

That error is thrown on the line where I do the return chain of the interceptor.

public OkHttpClient getClient(
            int readTimeOut,
            int connectTimeOut,
            Integer delayMiliSeconds) {

        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder()
                .readTimeout(readTimeOut, TimeUnit.MILLISECONDS)
                .writeTimeout(readTimeOut, TimeUnit.MILLISECONDS)
                .connectTimeout(connectTimeOut, TimeUnit.MILLISECONDS);

        okHttpClient.addInterceptor(new Interceptor() {
            @NonNull
            @Override
            public okhttp3.Response intercept(@NonNull Chain chain) throws IOException {
                Request original = chain.request();
                Request.Builder requestBuilder = original.newBuilder();

                Request request = requestBuilder.build();
                if (original.method().equals("GET")) {
                    HttpUrl originalHttpUrl = original.url();
                    HttpUrl url = originalHttpUrl.newBuilder()
                            .addQueryParameter("param", "some_param")
                            .build();
                    requestBuilder.url(url);
                    request = requestBuilder.build();
                }

                if(delayMiliSeconds != null && delayMiliSeconds > 0){
                    SystemClock.sleep(delayMiliSeconds);
                }

                return chain.withReadTimeout(readTimeOut, TimeUnit.MILLISECONDS)
                        .withConnectTimeout(connectTimeOut, TimeUnit.MILLISECONDS)
                        .proceed(request);
            }
        });

        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.level(HttpLoggingInterceptor.Level.BODY);
            okHttpClient.addInterceptor(interceptor);
        }

        return okHttpClient.build();
    }

I tried assigning the time directly to the Builder of the OkHttpClient and directly to the chain in the return.

I thank you in advance for any suggestion.

Fernando
  • 43
  • 1
  • 5

1 Answers1

1

I need a timeout of at least 2 minutes. But that exception is thrown at 10 seconds or so.

10 seconds is the default timeout for OkHttpClient read, write and connect. Try increasing the timeout seconds in the builder as shown here:

OkHttpClient client = new OkHttpClient().newBuilder()
    .connectTimeout(180, TimeUnit.SECONDS)
    .writeTimeout(180, TimeUnit.SECONDS)
    .readTimeout(180, TimeUnit.SECONDS)
    .build();
Petros Erk
  • 11
  • 1