title: How can I have a default onResponse for all my Retrofit2 calls?
body: <p>So I'm using Retrofit 2 to create all my API calls. </p>
  
  <p>Here is some example code within my project. First for the Api.</p>
  
  <pre><code>public class Api {
  public static final String BASE_URL = "http://www.api.com/";
  public static final String kApiUserLogin = "user/login/";
  public static final String kApiUserRegister = "user/register/";
  
  private static ApiInterface sService;
  
  public static ApiInterface client() {
      if (sService == null) {
          Gson gson = new GsonBuilder()
                  .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                  .create();
  
          OkHttpClient okHttpClient = new OkHttpClient.Builder()
                  .build();
  
          Retrofit retrofit = new Retrofit.Builder()
                  .baseUrl(BASE_URL)
                  .client(okHttpClient)
                  .addConverterFactory(GsonConverterFactory.create(gson))
                  .build();
  
          sService = retrofit.create(ApiInterface.class);
      }
  
      return sService;
  }
  
  public interface ApiInterface {
      @POST(kApiUserLogin)
      Call&lt;UserResponse&gt; loginUser(@Body UserDTO userDTO);
  
      @POST(kApiUserRegister)
      Call&lt;UserResponse&gt; registerUser(@Body UserDTO userDTO);
  
  }}
  }
  </code></pre>
  
  <p>And then this is how I use it: </p>
  
  <pre><code>                    Call&lt;UserResponse&gt; call = Api.client().registerUser(user);
                      call.enqueue(new Callback&lt;UserResponse&gt;() {
                          @Override
                          public void onResponse(Response&lt;UserResponse&gt; response) {
                              UserResponse userResponse = response.body();
                              if (userResponse.getError() == null) {
                                  UserDTO userDto = userResponse.getUser();
                                  User.getInstance().setUserDTO(userDto);
  
                                  Intent intent = new Intent(getActivity(), MainActivity.class);
                                  startActivity(intent);
                              } else {
                                  dialog
                                          .setTitleText("Error")
                                          .setContentText(userResponse.getError())
                                          .setConfirmText("OK")
                                          .changeAlertType(SweetAlertDialog.ERROR_TYPE);
                              }
                          }
  
                          @Override
                          public void onFailure(Throwable t) {
                              Log.d("CallBack", " Throwable is " + t);
                              dialog.hide();
                          }
                      });
  
                      Call&lt;UserResponse&gt; call = Api.client().loginUser(user);
                      call.enqueue(new Callback&lt;UserResponse&gt;() {
                          @Override
                          public void onResponse(Response&lt;UserResponse&gt; response) {
                              UserResponse userResponse = response.body();
                              if (userResponse.getError() == null) {
                                  UserDTO userDto = userResponse.getUser();
                                  User.getInstance().setUserDTO(userDto);
  
                                  Intent intent = new Intent(getActivity(), MainActivity.class);
                                  startActivity(intent);
                              } else {
                                  dialog
                                          .setTitleText("Error")
                                          .setContentText(userResponse.getError())
                                          .setConfirmText("OK")
                                          .changeAlertType(SweetAlertDialog.ERROR_TYPE);
                              }
                          }
  
                          @Override
                          public void onFailure(Throwable t) {
                              Log.d("CallBack", " Throwable is " + t);
                              dialog.hide();
                          }
                      });
                  }
  </code></pre>
  
  <p>Now the question is how can I make it so that I can reuse a piece of code for the <code>else</code> section of <code>if (userResponse.getError() == null)</code> inside the <code>onReponse</code>? Bare un mind this is just an example. There are many api calls throughout the app, so it´s not like I just can make a method to be called below this 2 methods.</p>
  
  <p>I was looking more for a default onResponse handler or something like that inside the Api class??</p>
  
  <p>At the same time, I´d like to do the same for the <code>onFailure</code> of course.</p>
  
  <p>Maybe I´m overcomplicating something very simple...</p>