49

I referred this link but I can't seem to implement for mine

I am using

 compile 'com.squareup.retrofit2:retrofit:2.0.2'
 compile 'com.squareup.retrofit2:converter-gson:2.0.2'

I am using the below code, How to set timeout for this !

public class ApiClient {

    public static final String BASE_URL = Constants.BaseURL;
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}
Community
  • 1
  • 1
Devrath
  • 42,072
  • 54
  • 195
  • 297

5 Answers5

100

Configure OkHttpClient for timeout option. Then use this as client for Retrofit.Builder.

final OkHttpClient okHttpClient = new OkHttpClient.Builder()
    .connectTimeout(20, TimeUnit.SECONDS)
    .writeTimeout(20, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build();

Use this okHttpClient for Retrofit#Builder

Retrofit.Builder()
    .client(okHttpClient);

Official OkHttp documentation about timeout is here

Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56
  • what is the difference between connect, write and read time out? thank you :) – ladytoky0 May 14 '22 at 00:40
  • 2
    Connect Timeout - The maximum time in making the initial connection to the server. Write Timeout - The maximum time you allow to write (send) the request data to the service. Read Timeout - The time you wait for the response. @ladytoky0 Hope this helps you :) – Alex Chengalan May 16 '22 at 06:18
  • 1
    thank you very much! so much clear now :D – ladytoky0 May 16 '22 at 20:28
8

try below code, it sét timeout is 20 seconds and readTimeout is 30 seconds

 private OkHttpClient getRequestHeader() {
        OkHttpClient httpClient = new OkHttpClient();
        httpClient.setConnectTimeout(20, TimeUnit.SECONDS);
        httpClient.setReadTimeout(30, TimeUnit.SECONDS);

        return httpClient;
    }

Then

public class ApiClient {

    public static final String BASE_URL = Constants.BaseURL;
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(getRequestHeader())
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
        }
    }
ThaiPD
  • 3,503
  • 3
  • 30
  • 48
  • I'm curious why you named your method `getRequestHeader()`? I'd suggest `getHttpClient()` would be a better name. – k2col Sep 03 '17 at 19:20
  • 1
    I am getting setConnectTimeout() & setReadTimeout() can't be resolved. Imported package okhttp3.OkHttpClient. Please help me, i am new to retrofit/okhttp – VVB Sep 07 '17 at 07:51
1

I have used bellow like in Kotlin with MVVM Model..

var okHttpClient: OkHttpClient? = OkHttpClient.Builder()
    .connectTimeout(60, TimeUnit.SECONDS)
    .readTimeout(60, TimeUnit.SECONDS)
    .writeTimeout(60, TimeUnit.SECONDS)
    .build()

private val api = Retrofit.Builder()
    .baseUrl(baseurl)
    .addConverterFactory(GsonConverterFactory.create())
    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .client(okHttpClient)
    .build()
    .create(Api::class.java);
Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
0

If you are using "com.squareup.retrofit2:retrofit:2.4.0" retrofit version > 2 then you try this one:

private OkHttpClient getRequestHeader() 
{
    OkHttpClient okHttpClient = new OkHttpClient.Builder()
        .readTimeout(60, TimeUnit.SECONDS)
        .connectTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(20, TimeUnit.SECONDS)
        .build();

    return okHttpClient;
}
pritaeas
  • 2,073
  • 5
  • 34
  • 51
0

In Kotlin you can Configure timeout for Retrofit by

Create OkHttpClient object with time in seconds (The default value is 10 seconds)

private val okHttpClient = OkHttpClient.Builder()
    .connectTimeout(30, TimeUnit.SECONDS)
    .writeTimeout(30, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .build()

Then use this object for Retrofit Builder

private val retrofit = Retrofit.Builder()
    .addConverterFactory(ScalarsConverterFactory.create())
    .client(okHttpClient)
    .baseUrl(BASE_URL)
    .build()

And import these

import java.util.concurrent.TimeUnit
import okhttp3.OkHttpClient
import retrofit2.Retrofit
Marawan Mamdouh
  • 584
  • 1
  • 6
  • 15