title: Set timeout in retrofit Android?
body: <p>Actually i'm sending a txt file via Retrofit to my server and all is working fine i'm even catching onFailure method if like there is no connection.</p>
  
  <p>But the issue comes when i go out of Wi-Fi zone then i send the file and reenter in the Wi-Fi zone the file is going to be send but the onFailure method is activated. </p>
  
  <p>So how can i sent like a timeout for onResponse method or something like that?</p>
  
  <p>Here is my sendPost method from Activity:</p>
  
  <pre><code> @SuppressLint("DefaultLocale")
      public void sendPost() {
          @SuppressLint({"SdCardPath", "DefaultLocale"}) final File file = new File("/data/data/com.example.igardini.visualposmobile/files/"+String.format("%03d", Integer.valueOf(nTerminalino))+"_"+nTavoli.getText().toString()+".txt");
  
          MultipartBody.Builder builder = new MultipartBody.Builder();
          builder.setType(MultipartBody.FORM);
  
          RequestBody fbody = RequestBody.create(MediaType.parse("text/*"), file); builder.addFormDataPart(String.format("%03d", Integer.valueOf(nTerminalino))+"_"+nTavoli.getText().toString(), file.getName(),fbody);
  
          MultipartBody requestBody = builder.build();
  
          APIService apiService = ApiUtils.getAPIService(ipCASSA);
  
          Call&lt;Void&gt; call = apiService.savePost(requestBody);
  
          call.enqueue(new Callback&lt;Void&gt;() {
              @Override
              public void onResponse(@NonNull Call&lt;Void&gt; call, @NonNull Response&lt;Void&gt; response) {
                  if (response.isSuccessful()) {
                      Log.i("RESPONSE: ", response.toString());
                      Print();
                      file.delete();
                      dialogLoading.dismiss();
                      Runtime.getRuntime().gc();
                  } else {
                          //
                  }
              }
  
              @Override
              public void onFailure(@NonNull Call&lt;Void&gt; call, @NonNull Throwable t) {
                  Log.e("TAG", t.toString());
                  MediaPlayer mpFound = MediaPlayer.create(pterm.this,R.raw.errorsound);
                  mpFound.start();
                  Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                  if (v.hasVibrator()) {
                      v.vibrate(6000);
                  } else {
                      //
                  }
                  new GlideToast.makeToast(pterm.this, "CONNESSIONE FALLITA!", GlideToast.LENGTHLONG, GlideToast.FAILTOAST).show();
                  dialogLoading.dismiss();
              }
          });
      }
  </code></pre>
  
  <p>While here is ApiUtils class:</p>
  
  <pre><code>class ApiUtils {
  
      private ApiUtils() {}
  
  
  
      static APIService getAPIService(String ipCASSA) {
  
          String BASE_URL = "http://"+ipCASSA+"/WebQuery/";
  
          return RetrofitClient.getClient(BASE_URL).create(APIService.class);
      }
  }
  </code></pre>
  
  <p>And here is RetrofitClient class:</p>
  
  <pre><code>public class RetrofitClient {
  
      private static Retrofit retrofit = null;
  
      public static Retrofit getClient(String baseUrl) {
          if (retrofit==null) {
              retrofit = new Retrofit.Builder()
                      .baseUrl(baseUrl)
                      .addConverterFactory(GsonConverterFactory.create())
                      .build();
          }
          return retrofit;
      }
  }
  </code></pre>
  
  <p>While here is APIService</p>
  
  <pre><code>public interface APIService {
  
      @POST("UPD.aspx?CART=PTERM")
      Call&lt;Void&gt; savePost(@Body RequestBody text);
  }
  </code></pre>