Long Ranger
Long Ranger

Reputation: 6008

How to set the whole timeout in OkHttpClient?

I tried to google find the related issue but no luck. I know how to set the connectionTimeout, readTimeout, writeTimeout but I want to set the whole timeout for these value because sometimes the value between these three are vary.

The similar is this one

How to set connection timeout with OkHttp

But the one I want is something like that

builder.allTimeout(50, TimeUnit.SECONDS)

Upvotes: 1

Views: 410

Answers (3)

Shahab Rauf
Shahab Rauf

Reputation: 3931

I couldn't find any method that can set timeout for all as you can check it in documentation.

http://square.github.io/okhttp/3.x/okhttp/

Further, I couldn't find a reason that why do you want to achieve this.

Try some below workaround that may solve your problem.

Initialise a const value

final int ALL_TIMEOUT = 30;

new OkHttpClient().newBuilder() 
    .readTimeout(ALL_TIMEOUT, TimeUnit.SECONDS)
    .connectTimeout(ALL_TIMEOUT, TimeUnit.SECONDS);
    .writeTimeout(ALL_TIMEOUT, TimeUnit.SECONDS);

OR

Make a wrapper that will initialise your builder and just pass a value for all timeouts and wrapper will initialise all timeouts itself and give you the builder object.

Upvotes: 1

Ekta Bhawsar
Ekta Bhawsar

Reputation: 746

In my case the problem was that I had an implementation of SSL and I was creating custom OkHttpClient that was overriding my further implementation. I was setting there timeout and I was not able to change it. I have forgot about it and spend a lot of time to find the problem which was simple. Take a look at your own code and check if you have no other implementation of OkHttpClient which is overriding your timeouts.

Upvotes: 0

Si Thu
Si Thu

Reputation: 39

OkHttpClient.Builder builder = new OkHttpClient().newBuilder() 
    .readTimeout(30, TimeUnit.SECONDS)
    .connectTimeout(30, TimeUnit.SECONDS);

Upvotes: 0

Related Questions