1

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
}
Rares
  • 597
  • 1
  • 5
  • 21
  • If you are using the HttpClient API in the Android SDK, **stop**. That has been deprecated and removed. Use something else (e.g., OkHttp). If you are using the standalone Apache HttpClient library for Android, you need to specify which version, as that API has shifted over the years. – CommonsWare Jun 18 '17 at 23:21
  • my bad. I wanted to write okhttp . I am using 'com.squareup.okhttp3:okhttp:3.8.0' – Rares Jun 18 '17 at 23:23
  • Um, your question is all about HttpClient, though. If you are actually using OkHttp instead, [here is how you configure timeouts](https://github.com/square/okhttp/wiki/Recipes#timeouts). – CommonsWare Jun 18 '17 at 23:24
  • Can you please post the answer as response so I can vote it? Thx! – Rares Jun 18 '17 at 23:26

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491