'OkHttp/Retrofit default timeout

I was wondering how many seconds should I set to my retrofit client.

  1. How many seconds should I use as default timeout?
  2. What is the default timeout for OkHttp/Retrofit, should we let default values?


Solution 1:[1]

  1. It shouldn't take forever and not too short. IMHO, it should be between 10s and 30s.
  2. Default connect time out setting that Retrofit gives you (if you haven't specified http client explicitly) is 15 seconds.

Source:

OkHttpClient defaultClient() {   
    OkHttpClient client = new OkHttpClient();
    client.setConnectTimeout(15, TimeUnit.SECONDS);
    client.setReadTimeout(15, TimeUnit.SECONDS);
    client.setWriteTimeout(15, TimeUnit.SECONDS);
    return client;
}
  1. I get this feeling Google is using 30 seconds. Not sure.

Solution 2:[2]

You can use

OkHttpClient okHttpClient = new OkHttpClient.Builder()  
        .connectTimeout(1, TimeUnit.MINUTES)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(15, TimeUnit.SECONDS)
        .build();

Retrofit.Builder builder = new Retrofit.Builder()  
        .baseUrl("http://10.0.2.2:3000/")
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create());

for more details go to: https://futurestud.io/tutorials/retrofit-2-customize-network-timeouts

Solution 3:[3]

Solution 4:[4]

I'am using it like this in my RetrofitApiClient . okhttp version 3.4.1

public class RetrofitApiClient {

         private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
                .connectTimeout(15, TimeUnit.SECONDS)
                .readTimeout(15L, TimeUnit.SECONDS)
                .writeTimeout(15L, TimeUnit.SECONDS);

        public  void someMethod() {
            OkHttpClient client = httpClient.build();
        }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Gomez Rico
Solution 2 Arunjith R S
Solution 3 cgr
Solution 4 Igor Vuković