How to set timeout in Retrofit library?
You can set timeouts on the underlying HTTP client. If you don't specify a client, Retrofit will create one with default connect and read timeouts. To set your own timeouts, you need to configure your own client and supply it to the RestAdapter.Builder
.
An option is to use the OkHttp client, also from Square.
1. Add the library dependency
In the build.gradle, include this line:
compile 'com.squareup.okhttp:okhttp:x.x.x'
Where x.x.x
is the desired library version.
2. Set the client
For example, if you want to set a timeout of 60 seconds, do this way for Retrofit before version 2 and Okhttp before version 3 (FOR THE NEWER VERSIONS, SEE THE EDITS):
public RestAdapter providesRestAdapter(Gson gson) {
final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(60, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(60, TimeUnit.SECONDS);
return new RestAdapter.Builder()
.setEndpoint(BuildConfig.BASE_URL)
.setConverter(new GsonConverter(gson))
.setClient(new OkClient(okHttpClient))
.build();
}
EDIT 1
For okhttp versions since 3.x.x
, you have to set the dependency this way:
compile 'com.squareup.okhttp3:okhttp:x.x.x'
And set the client using the builder pattern:
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.build();
More info in Timeouts
EDIT 2
Retrofit versions since 2.x.x
also uses the builder pattern, so change the return block above to this:
return new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
If using a code like my providesRestAdapter
method, then change the method return type to Retrofit.
More info in Retrofit 2 — Upgrade Guide from 1.9
ps: If your minSdkVersion is greater than 8, you can use TimeUnit.MINUTES
:
okHttpClient.setReadTimeout(1, TimeUnit.MINUTES);
okHttpClient.setConnectTimeout(1, TimeUnit.MINUTES);
For more details about the units, see TimeUnit.
Set timeout in retrofit Android?
Just add as below
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());
also you can check this easy tutorial.
How to set timeout in Retrofit-2.0+ android
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
How can I set connection timeout in Retrofit library for Android?
It is working.
public class OperatingApiClient {
public static final String BASE_URL = "http://172.16.2.39/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
public static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(120, TimeUnit.SECONDS)
.connectTimeout(120, TimeUnit.SECONDS)
.build();
}
Setting custom connection timeout in retrofit not working
You are used wrong httpclient when create retrofit object
You have to add below lines of codes inside createService(Class<S> serviceClass, Context context)
httpClient.readTimeout(2, TimeUnit.MINUTES)
.connectTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES);
So your createService(Class<S> serviceClass, Context context)
would be
public static <S> S createService(Class<S> serviceClass, Context context) {
if (!httpClient.interceptors().isEmpty()) {
httpClient.interceptors().clear();
}
httpClient.addInterceptor(new NetworkConnectionInterceptor(context));
httpClient.addInterceptor(setLogger());
httpClient.readTimeout(2, TimeUnit.MINUTES)
.connectTimeout(2, TimeUnit.MINUTES)
.writeTimeout(2, TimeUnit.MINUTES);
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request.Builder requestBuilder = original.newBuilder()
MyApplication.getInstance().getApiCredential())
.header("LocalSystemDate", MyApplication.getInstance().getCurrentDateAndTimeWithTimeZone())
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
OkHttpClient client = httpClient.build();
Retrofit retrofit = builder.client(client).build();
return retrofit.create(serviceClass);
}
It's will work now. try it .
How to increase upload time for retrofit library in android
Here try this: In below code you will pass your custom OKHTTP client with your custom timeout
public class RetrofitClient {
private static final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.build();
public static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
You also need this dependency:
implementation 'com.squareup.okhttp3:okhttp:3.10.0'// OKHTTP \\
Add this to your gradle file
Related Topics
How to Check Certificate Name and Alias in Keystore Files
Getting Java.Net.Sockettimeoutexception: Connection Timed Out in Android
Java Program That Runs Commands with Linux Terminal
Why Does Priorityqueue.Tostring Return the Wrong Element Order
Picking a Random Element from a Set
How to Store More Than One String in a Map
How to Read System Environment Variable in Spring Applicationcontext
How to Generate a Random Biginteger Value in Java
How to Synchronize a Static Variable Among Threads Running Different Instances of a Class in Java
Java Compiled Classes Contain Dollar Signs
Failed to Install Android-Sdk: "Java.Lang.Noclassdeffounderror: Javax/Xml/Bind/Annotation/Xmlschema"
Android - Change App Theme on Onclick
How to Achieve Javafx Mouse Event "Push and Hold"
How to Switch JPAnels Inside a Jframe
What's the Syntax for Mod in Java
How to Move My Jmenubar to the Screen Menu Bar on MAC Os X