I am using retrofit library to upload file to server. When file was uploaded it shows error:: timeout
. How to increase upload time so that I can upload full file in server.
Asked
Active
Viewed 2,373 times
2

Sudip Sadhukhan
- 1,784
- 12
- 21
-
3Possible duplicate of [How to set timeout in Retrofit library?](https://stackoverflow.com/questions/29380844/how-to-set-timeout-in-retrofit-library) – Gowthaman M Jul 10 '18 at 05:52
4 Answers
9
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

Rahul Singh Chandrabhan
- 2,531
- 5
- 22
- 33
-
-
You can use multipart for file transfer. The web-service will maintain the connection until your file gets uploaded, unless there is a timeout from server side. – Rahul Singh Chandrabhan May 19 '20 at 16:52
-
Can you help on this : https://stackoverflow.com/questions/61896772/how-to-handle-retrofit-socket-timeout-for-uploading-files-in-android-kotlin – SARATH V May 19 '20 at 17:05
-
2
Add the following class in your project.
public class Api{
static Gson gson = new GsonBuilder()
.setLenient()
.create();
public static Retrofit adapter = new Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL) //Set the Root URL
.addConverterFactory(GsonConverterFactory.create(gson))
.client(configureTimeouts())
.build(); //Finally building the adapter
public static OkHttpClient configureTimeouts() {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(90, TimeUnit.SECONDS) // Set your timeout duration here.
.writeTimeout(90, TimeUnit.SECONDS)
.readTimeout(90, TimeUnit.SECONDS)
.build();
return okHttpClient;
}
}
In the above configureTimeouts() function, you can set the duration after which the timeout occurs according to your requirements.
Also, don't forget to put these dependencies in your app level build.gradle file
compile 'com.squareup.retrofit2:retrofit:2.4.0'
compile 'com.squareup.retrofit2:converter-gson:2.4.0'

Vishal Gupta
- 81
- 5
0
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();
}

Zayniddin Mamarasulov
- 253
- 1
- 3
- 12
0
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder().readTimeout(120, TimeUnit.SECONDS).connectTimeout(120, TimeUnit.SECONDS) .writeTimeout(120, TimeUnit.SECONDS); private static Retrofit retrofit;
static {
Builder addConverterFactory = new Builder().baseUrl(API_BASE_URL).addConverterFactory(GsonConverterFactory.create());
builder = addConverterFactory;
retrofit = addConverterFactory.build();
}
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null, null);
}
public static <S> S createService(Class<S> serviceClass, String username, String password) {
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
return createService(serviceClass, null);
}
return createService(serviceClass, Credentials.basic(username, password));
}
public static <S> S createService(Class<S> serviceClass, String authToken) {
if (!TextUtils.isEmpty(authToken)) {
AuthenticationInterceptor interceptor = new AuthenticationInterceptor(authToken);
if (!httpClient.interceptors().contains(interceptor)) {
httpClient.addInterceptor(interceptor);
builder.client(httpClient.build());
retrofit = builder.build();
}
}
return retrofit.create(serviceClass);
}

Janardan Dash
- 35
- 7