改造通用响应处理程序

问题描述 投票:0回答:2

我希望用单一方法处理我的所有回复。目的是当响应码不为3时召回服务,当响应码为3时我打算先刷新令牌,然后召回相同的服务。

我创建了一个

BaseCallback
类来捕获一种方法,但我看不到日志,也无法捕获断点。

BASECALLBACK.class

public class BaseCallBack<T> implements Callback<T> {

 @Override
 public void onResponse(Call<T> call, Response<T> response) {

     if (!response.isSuccessful()){
         Log.d("BaseCallback","Response Fail");
     }
 }

 @Override
 public void onFailure(Call<T> call, Throwable t) {
     t.toString();
 }
}

通话方法

 ApiManager.getInstance().getUsers().enqueue(new BaseCallBack<List<User>>() {
            @Override
            public void onResponse(Call<List<User>> call, Response<List<User>> response) {

                if (response.isSuccessful()){

                }
            }

            @Override
            public void onFailure(Call<List<User>> call, Throwable t) {

            }
        });

我只想用单一方法处理我的服务。

android performance retrofit response retrofit2
2个回答
9
投票

您的起点很好 - 您有一个

ApiManager
,这是您正在寻找的单个点 -
class
,而不是
method
(在这种情况下,方法不应该是单个接触点,它会使您的代码不可读并且以后更难调试。

从这里开始,最好使用您自己的自定义接口,并按照您希望的方式从调用请求的位置实现它,您可以在那里处理您想要的东西,这是一个“非常通用”的示例,应该修复一些问题让你继续前进。 请注意,这仍然需要您工作 - 调整并添加您需要的内容。

这就是你所需要的界面(非常基本,你可以添加东西)

public interface CustomCallListener<T> { public void getResult(T object); }

这就是你应该在 ApiManager 中使用它的方式 - 它接收你的接口作为携带预期对象类型的参数,当响应返回时执行你需要的操作 - 解析它,剪切它,等等 - 并将其转换为正确的对象,这个例子使用了一个 String 响应和一个 List 返回对象,你可以期待任何你想的并相应地解析它,Retrofit2 允许你直接解析 JSON 字符串(使用 GSON 或其他一些库),所以你可以决定在这里使用什么 -如果你不明白我的意思 - 请阅读它。

这也是我添加断点和

Log.

调用来调试您得到的响应的地方,您还可以分解

rawResponse
以获取标题和其他内容。

class ApiManager { // .. other stuff you need... public void getSomeList(final CustomCallListener<List<SomeObject>> listener) { Call<ResponseBody> request = serviceCaller.getSomeInfo(userid); //...other stuff you might need... request.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> rawResponse) { try { String response = rawResponse.body().string(); //...other stuff you might need... //...do something with the response, convert it to //return the correct object... SomeObject object = new SomeObject(response); listener.getResult(object); } catch (Exception e) { // .. the response was no good... listener.getResult(null); } } @Override public void onFailure(Call<ResponseBody> call, Throwable throwable) { // .. the response was no good... listener.getResult(null); } }); } }

最后,这是您应该在代码中的任何位置使用的内容 - 这允许您在那里实现回调,并通过从 
ApiManager

返回的内容来处理您需要的任何内容。


ApiManager.getInstance().getSomeList(new CustomCallListener<List<SomeObject>>() { @Override public void getResult(List<SomeObject> objects) { if (null != objects) { // check objects and do whatever... } else { // ... do other stuff .. handle failure codes etc. } } });

注意事项

如前所述 - 这是一个非常通用的框架,可以进行很大的修改(向具有不同返回类型的接口添加更多方法来处理异常、错误响应、其他对象,向同一方法添加更多参数以处理更多选项等。 )更多地了解该主题,注意传递 null 对象,使用 try 和 catch 以避免崩溃。

希望这有帮助!


0
投票

https://github.com/Mohammad-Taqi/Generic-Retrofit-Implementation

© www.soinside.com 2019 - 2024. All rights reserved.