0

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;
    }
bircastri
  • 2,169
  • 13
  • 50
  • 119
  • Possible duplicate of https://stackoverflow.com/questions/39219094/sockettimeoutexception-in-retrofit – RoyalGriffin Jun 29 '18 at 09:24
  • Possible duplicate of [SocketTimeoutException in Retrofit](https://stackoverflow.com/questions/39219094/sockettimeoutexception-in-retrofit) – RoyalGriffin Jun 29 '18 at 09:24
  • 1
    Possible duplicate of [How to set timeout in Retrofit library?](https://stackoverflow.com/questions/29380844/how-to-set-timeout-in-retrofit-library) – Jinesh Malavia Jun 29 '18 at 09:33

1 Answers1

4

You can increase your timeout by specifying different timeouts in your OkHttpClient which is used to build your retrofit instance.

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;
}
Abhijeet Kumar
  • 343
  • 2
  • 8