我如何为我的所有 Retrofit2 调用设置默认的 onResponse?

How can I have a default onResponse for all my Retrofit2 calls?

所以我正在使用 Retrofit 2 创建我所有的 API 调用。

这是我项目中的一些示例代码。首先为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();
                        }
                    });
                }

现在的问题是我怎样才能做到这样才能在 onReponse 中为 if (userResponse.getError() == null)else 部分重复使用一段代码?请注意,这只是一个例子。整个应用程序中有很多 api 调用,所以我不能只在这 2 个方法下面创建一个要调用的方法。

我一直在寻找 Api class??

中的默认 onResponse 处理程序或类似的东西

与此同时,我当然想为 onFailure 做同样的事情。

也许我把非常简单的事情复杂化了...

如果我理解正确,这就足够了。

   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);
        }
    }