Retrofit2 Android increase Timeout connection
Retrofit2 Android increase Timeout connection
I'm building a Service to send data throught web services in Android using retrofit2.
Now this is the code:
private void checkUnsyncedRecords() {
final DbLayer db = DatabaseHelper.getDatabase(getApplicationContext(), false);
LinkedList<SensorData> listaDatiUnsunc = db.fetchSensorData(null, false);
WebService webService = WebServiceHandler.getService();
//endregion
//region Alerts
if (listaDatiUnsunc.size() > 0) {
JsonArray readAlerts = new GsonBuilder().create().toJsonTree(listaDatiUnsunc).getAsJsonArray();
//create request body
JsonObject body = WebServiceHandler.getDefaultBody();
body.addProperty(WebServiceHandler.ID_CLINICAL_DOCUMENT, 70);
body.add(WebServiceHandler.VALORI, readAlerts);
//Log.i(TAG, body.toString());
try {
Call<BaseMessage<JsonElement>> request = webService.insertDatiCalza(body);
Response<BaseMessage<JsonElement>> response = request.execute();
if (response.isSuccessful()) {
BaseMessage<JsonElement> message = response.body();
if (message != null) {
if (message.getStatus() == MessageStatus.SUCCESS) {
Log.d(TAG, "SensorValue sync succeeded");
//AGGIORNO LO STATO DI AGGIORNAMENTO DI TUTTE LE RILEVAZIONI
for (SensorData sens: listaDatiUnsunc) {
sens.setSync(true);
db.updateSensorData(sens);
}
} else {
Log.e(TAG, "SensorValue sync failed: " + message.getMessage() +
" (code: " + message.getStatus() + ")");
}
} else {
Log.e(TAG, "SensorValue sync: reply message is null.");
}
} else {
Log.e(TAG, "SensorValue sync: request failed (code: " +
response.code() + ", body: " + response.message() + ").");
}
} catch (Exception e) {
Log.e(TAG, "SensorValue sync call", e);
} finally {
}
}
//endregion
}
this code works, but sometimes the LinkedList contains more value and I have this exception if it size is big.
java.net.SocketTimeoutException
Now there is a way to increase the Timeout connection ?
EDIT
This is the instance of my Retrofit class:
public static WebService getService() {
if (webService == null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson)).baseUrl(BASE_URL).build();
webService = retrofit.create(WebService.class);
}
return webService;
}
Possible duplicate of SocketTimeoutException in Retrofit
– RoyalGriffin
Jun 29 at 9:24
Possible duplicate of How to set timeout in Retrofit library?
– Jinesh Malavia
Jun 29 at 9:33
1 Answer
1
You can increase your timeout by specifying different timeouts in your OkHttpClient
which is used to build your retrofit
instance.
OkHttpClient
retrofit
val okhttpclient = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS);
.build()
And use this in to create retrofit instance.
Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
.callFactory(httpClientBuilder.build())
.build()
Edit
Use this for generating your WebService
public static WebService getService() {
if (webService == null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
OkHttpClient okhttpclient = OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS);
.build()
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson)).baseUrl(BASE_URL).callFactory(okhttpclient).build();
webService = retrofit.create(WebService.class);
}
return webService;
}
I have just edit my first question with the instance of retrofit.
– bircastri
Jun 29 at 9:35
Simply create an
okhttpclient
instance and add it as .callFactory(okhttpclient)
to your retrofit
instance with your custom timeouts for your okhttp client.– Abhijeet Kumar
Jun 29 at 9:39
okhttpclient
.callFactory(okhttpclient)
retrofit
should it not be
.callFactory(okhttpclient)
?– Tim Castelijns
Jun 29 at 9:42
.callFactory(okhttpclient)
I don't understand how can I write the code OKHttpClient
– bircastri
Jun 29 at 9:44
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of stackoverflow.com/questions/39219094/…
– RoyalGriffin
Jun 29 at 9:24