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;
}
}
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
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;
}
}
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);
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;
}
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
Related
I am developing an android app using external API servers.
Because I use 2 servers, I have 2 retrofit services.
In my "RemoteDataSource" class's constructor, I make the service objects like:
public RemoteDataSource() {
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
myService1 = new Retrofit.Builder()
.baseUrl(URL_1)
.client(okHttpClient)
.build()
.create(MyService1.class);
myService2 = new Retrofit.Builder()
.baseUrl(URL_2)
.client(okHttpClient) // my question is here!!!
.build()
.create(MyService2.class);
}
What I want to know is...
Can I use one "OkHttpClient" on both services?
Is there any network issue?
or Should I make another OkHttpClient object like "okHttpClient2" and assign it to "myService2"?
I want to set Retrofit connection timeout unlimited instead of static timeout
connection.how can I do it ?
This is my code...
public static Retrofit getRetrofitInstance() {
if (RetrofitInstance==null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(100, TimeUnit.SECONDS)
.connectTimeout(100, TimeUnit.SECONDS)
.build();
RetrofitInstance = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient)
.build();
}
return RetrofitInstance;
}
Retrofit does not allow developers to remove timeout completely, but you could set TimeUnit.HOURS to use big value for your timeout. For example:
final OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(Integer.MAX_VALUE, TimeUnit.HOURS)
.connectTimeout(Integer.MAX_VALUE, TimeUnit.HOURS)
.build();
Used Integer.MAX_VALUE for this example, which is big enough value to act as an infinite timeout.
Good luck :)
My app is using Retrofit+okhttp.
Sometimes, it throw SocketTimeoutException just through
1s~3s after request but my okhttp timeout is setting 10s.
It's my code.
private static OkHttpClient getClient() {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.addInterceptor(headerinterceptor)
.build();
return client;
}
public static XXXX XXXX() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(XXXX)
.addConverterFactory(StatusProcessConverter.create(new GsonBuilder().setLenient().create()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(getClient())
.build();
XXXX methods = retrofit.create(XXXX.class);
return methods;
}
Can anyone help me ... why the sockettimeout occur less than 10s...
I am facing the issue of socket timeout exception while using retrofit 2.0.2 library and okhttp 2.3.0. I am trying to upload the image file which is between 500kb to 1.5mb it is uploading successfully.but when i tried to upload video file which is greater than 5mb i am getting this exception.
I used httpclient for connection settings as below.
public static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.SECONDS)
.writeTimeout(0, TimeUnit.SECONDS)
.readTimeout(0, TimeUnit.SECONDS)
.build();
please suggest me to upload larger files without this issue.Thanks in advance
you can provide the time in seconds as follows
public class ApiClient {
public static final String BASE_URL = "your_url";
public static Retrofit retrofit = null;
public static Retrofit getApiClient() {
if (retrofit == null) {
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
return new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
i've given 60 seconds
There can be two issues with this type of error.
Check Read & Write Timeout
val client = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build()
Check the Mime type you are sending. The backend developer may have filtered the accepted Mime type at their end. instead of MediaType.parse("multipart/form-data"). write the valid mime type for files like image/jpg or video/mp4
MediaType.parse("image/png")
I am using retrofit 2. I need to use 2 different Rest API, because they have different base URL, headers and cookies.
public class RestClient {
private static IRestApi REST_CLIENT;
static {
setupRestClient();
}
public static IRestApi get() {
return REST_CLIENT;
}
private static void setupRestClient() {
Interceptor interceptor = new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
//add some cookies & headers
}
};
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(GenericConstants.READ_TIMEOUT, TimeUnit.SECONDS)
.connectTimeout(GenericConstants.CONNECTION_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(GenericConstants.BASE_ENDPOINT_INSTAGRAM)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
REST_CLIENT = retrofit.create(IRestApi.class);
}
}
I am using it like so:
Call<Obj> call = RestClient.get().myMethod(params);
I want to create 2 clases like this, because I have 2 types of requests, 1 with headers and cookies and one plain, is this possible?
you can create method of retrofit url like
public Retrofit getAdapter(String url){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
return retrofit;
}
Use this adapter wherever you want.