2016-01-22 82 views
1

所以我使用Retrofit 2來創建我所有的API調用。如何爲我的所有Retrofit2調用設置默認的onResponse?

這是我的項目中的一些示例代碼。首先爲Api。

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<UserResponse> loginUser(@Body UserDTO userDTO); 

    @POST(kApiUserRegister) 
    Call<UserResponse> registerUser(@Body UserDTO userDTO); 

}} 
} 

然後這是我如何使用它:

    Call<UserResponse> call = Api.client().registerUser(user); 
        call.enqueue(new Callback<UserResponse>() { 
         @Override 
         public void onResponse(Response<UserResponse> 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<UserResponse> call = Api.client().loginUser(user); 
        call.enqueue(new Callback<UserResponse>() { 
         @Override 
         public void onResponse(Response<UserResponse> 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(); 
         } 
        }); 
       } 

現在的問題是我怎麼讓這個我可以重用的代碼爲if (userResponse.getError() == null)onReponseelse節?不知不覺,這只是一個例子。整個應用程序中有很多API調用,所以它不像我可以在這兩種方法下面調用一個方法。

我正在尋找更多的默認的onResponse處理程序或類似的Api類內?

與此同時,我當然想要爲onFailure做同樣的事情。

也許I'm過於複雜的東西很簡單......

回答

0

如果我理解正確的,這將是足夠的。

Call<UserResponse> call = Api.client().registerUser(user); 
    call.enqueue(new Callback<UserResponse>() { 
     @Override 
     public void onResponse(Response<UserResponse> response) { 
      processResponse(response.body()); 
     } 

     @Override 
     public void onFailure(Throwable t) { 
      Log.d("CallBack", " Throwable is " + t); 
      dialog.hide(); 
     } 
    }); 

    Call<UserResponse> call = Api.client().loginUser(user); 
    call.enqueue(new Callback<UserResponse>() { 
     @Override 
     public void onResponse(Response<UserResponse> response) { 
      processResponse(response.body()); 
     } 

     @Override 
     public void onFailure(Throwable t) { 
      Log.d("CallBack", " Throwable is " + t); 
      dialog.hide(); 
     } 
    }); 
} 

    private void processResponse(UserResponse userResponse) { 
     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); 
     } 
    } 
+0

嘿@Ozgur,謝謝你的回答。我一發布就編輯我的問題,也許你沒有看到我的編輯。我提到在不同類別的應用程序中有很多API調用。我正在尋找更多的默認onResponse和onFailure處理程序或類似的Api類內? – Jan

+0

我想這是最簡單的代碼塊改造可以提供。但我也想知道答案。 – Ozgur

+0

如果我在一個星期內沒有得到更好的答案,我會按你的方式選擇你的答案。謝謝! – Jan

相關問題