Rares
Rares

Reputation: 637

How can I set connection timeout for OkHttpClient? 2017

It is easy to say it's duplicate but it isn't.

I read many post about how to set the connection timeout in android but the post are 4-7 years old and I think that we all need an update about this topic because those methods are deprecated or no longer exist.

So the question is how can I set my connection timeout when I am waiting for a response from the server?

final Response response = httpClient.newCall(request).execute();

if (response.isSuccessful()) {
                          //success
} else {
       //unsuccessful
}

Upvotes: 1

Views: 1304

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006604

If you create your OkHttpClient through an OkHttpClient.Builder, there are connectTimeout(), readTimeout(), and writeTimeout() methods that you can call for the various timeout options.

If you need to override them for a specific HTTP request, call newBuilder() on your OkHttpClient. That gives you an OkHttpClient.Builder with the same settings as you used originally. You can override those as needed, and create a temporary OkHttpClient from the new Builder, using that for this one-off call.

Upvotes: 1

Related Questions