大神之光Android架构

【Android架构】基于MVP模式的Retrofit2+RXj

2019-02-15  本文已影响661人  欢子3824

前言

mvp框架也用了相当长的时间了,一般让人比较纠结的就是后台数据的处理问题。大多数的公司由于代码的不规范、经手人员太多等等原因,后台的代码相当混乱,接口返回的数据格式也五花八门,当然,如果你能直接让后台大哥改代码的话,就另当别论,大多数情况还是要Android端来背锅。这里,我们就聊聊这个。

一般套路

我们会直接复制接口返回的json,然后用插件转换为实体类(国际惯例,不贴get和set)

public class ShareModel {
 
    private int status;
    private String msg;
    private List<DataBean> data;
    public static class DataBean {
        private String id;
        private String wshare_name;
        private String wshare_head;
        private String wshare_content;
  }
}

进阶套路

后台返回的数据格式如下:

{
    "status": 1,
    "msg": "请求成功",
    "data": []
}

我们会定义一个BaseModel(国际惯例,不贴get和set)

public class BaseModel<T> implements Serializable {
    private int status;
    private String msg;
    private T data;
}

如果datalist的话,还会定义个BaseListModel,只是其中的dataList<T>而已。
然后,在ApiServer中定义接口

 
    @FormUrlEncoded
    @POST("/mapi/index.php?ctl=user&act=userbaseinfo")
    Observable<BaseModel<UserModel>> getUserInfo(@FieldMap Map<String, String> params);

presenter中使用

    /**
     * 获取用户详情
     *
     * @param params
     */
    public void getUserInfo(Map<String, String> params) {
        addDisposable(apiServer.getUserInfo(params), new BaseObserver<BaseModel<UserModel>>(baseView) {

            @Override
            public void onSuccess(BaseModel<UserModel> o) {
                baseView.onGetUserInfoSucc(o.getData());

            }

            @Override
            public void onError(String msg) {
                baseView.showError(msg);
            }
        });

    }

然后回调到activity或者fragment中处理,这部分就不详细说了,可以看看之前的文章。

这样看似没有问题,但是如果后台某个接口返回的数据的格式如下,

{
    "status": 1,
    "error": "请求成功",
    "data": []
}

有人说了,在BaseModelBaseListModel再加一个error字段不就好了?

如果数据是这样呢?

{
    "code": 1,
    "error": "请求成功",
    "data": []
}

可能这张图能表达你现在的心情


image.png

终极套路

虽然生活如此艰难,但是问题还是要解决的。

我们可以回想一下,http请求返回的是对象是ResponseBody,它是怎么转换为我们的实体类呢?
主要代码在这里

retrofit.addConverterFactory(GsonConverterFactory.create())

我们跟进去看看

public final class GsonConverterFactory extends Converter.Factory {
  /**
   * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
   * decoding from JSON (when no charset is specified by a header) will use UTF-8.
   */
  public static GsonConverterFactory create() {
    return create(new Gson());
  }

  /**
   * Create an instance using {@code gson} for conversion. Encoding to JSON and
   * decoding from JSON (when no charset is specified by a header) will use UTF-8.
   */
  @SuppressWarnings("ConstantConditions") // Guarding public API nullability.
  public static GsonConverterFactory create(Gson gson) {
    if (gson == null) throw new NullPointerException("gson == null");
    return new GsonConverterFactory(gson);
  }

  private final Gson gson;

  private GsonConverterFactory(Gson gson) {
    this.gson = gson;
  }

  @Override
  public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
      Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonResponseBodyConverter<>(gson, adapter);
  }

  @Override
  public Converter<?, RequestBody> requestBodyConverter(Type type,
      Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
    return new GsonRequestBodyConverter<>(gson, adapter);
  }
}

可以看到,主要逻辑是在GsonResponseBodyConverter里面

final class GsonResponseBodyConverter<T> implements Converter<ResponseBody, T> {
  private final Gson gson;
  private final TypeAdapter<T> adapter;

  GsonResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
    this.gson = gson;
    this.adapter = adapter;
  }

  @Override public T convert(ResponseBody value) throws IOException {
    JsonReader jsonReader = gson.newJsonReader(value.charStream());
    try {
      T result = adapter.read(jsonReader);
      if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
        throw new JsonIOException("JSON document was not fully consumed.");
      }
      return result;
    } finally {
      value.close();
    }
  }
}

可以看到,先是拿到字节流,然后调用TypeAdapterread方法,转换为我们的实体类,这个原理我们先不深究,后面有时间在讲。这里我们能不能做文章呢,答案是可以。

首先,这几个类都是final修饰的,不能被继承,不过没事,我们可以复制这几个类的代码,然后改个名字


image.png

其中,BaseConverterFactoryBaseRequestBodyConverter与源码一致,只需要修改类名即可。重点在BaseResponseBodyConverter

public class BaseResponseBodyConverter<T> implements Converter<ResponseBody, T> {
    private final Gson gson;
    private final TypeAdapter<T> adapter;

    BaseResponseBodyConverter(Gson gson, TypeAdapter<T> adapter) {
        this.gson = gson;
        this.adapter = adapter;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {

        String jsonString = value.string();
        try {
            JSONObject object = new JSONObject(jsonString);

            int code = object.getInt("code");
            if (code != 1) {
                String msg = object.getString("msg");
                if (TextUtils.isEmpty(msg)) {
                    msg = object.getString("error");
                }
                //异常处理
                throw new BaseException(msg, code);
            }

            return adapter.fromJson(object.getString("data"));

        } catch (JSONException e) {
            e.printStackTrace();
            //数据解析异常
            throw new BaseException(BaseException.PARSE_ERROR_MSG, BaseException.PARSE_ERROR);
        } finally {
            value.close();
        }
    }
}

判断的代码可以自己根据项目需要,自行添加

重要的事说三遍,这里的判断逻辑要根据实际情况写!这里的判断逻辑要根据实际情况写!这里的判断逻辑要根据实际情况写!

最近好几个人问我,为啥后台返回的json都拿到了,还走的onError ,请检查这里的代码!
BaseException

public class BaseException extends IOException {

    /**
     * 解析数据失败
     */
    public static final int PARSE_ERROR = 1001;
    public static final String PARSE_ERROR_MSG = "解析数据失败";

    /**
     * 网络问题
     */
    public static final int BAD_NETWORK = 1002;
    public static final String BAD_NETWORK_MSG = "网络问题";
    /**
     * 连接错误
     */
    public static final int CONNECT_ERROR = 1003;
    public static final String CONNECT_ERROR_MSG = "连接错误";
    /**
     * 连接超时
     */
    public static final int CONNECT_TIMEOUT = 1004;
    public static final String CONNECT_TIMEOUT_MSG = "连接超时";
    /**
     * 未知错误
     */
    public static final int OTHER = 1005;
    public static final String OTHER_MSG = "未知错误";


    private String errorMsg;
    private int errorCode;


    public String getErrorMsg() {
        return errorMsg;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public BaseException(String errorMsg, Throwable cause) {
        super(errorMsg, cause);
        this.errorMsg = errorMsg;
    }

    public BaseException(String message, Throwable cause, int errorCode) {
        super(message, cause);
        this.errorCode = errorCode;
        this.errorMsg = message;
    }

    public BaseException(String message, int errorCode) {
        this.errorCode = errorCode;
        this.errorMsg = message;
    }
}

修改BaseObserver代码,onNext中只处理成功回调,onError中处理各种异常

public abstract class BaseObserver<T> extends DisposableObserver<T> {

    protected BaseView view;

    private boolean isShowDialog;


    public BaseObserver(BaseView view) {
        this.view = view;
    }

    public BaseObserver(BaseView view, boolean isShowDialog) {
        this.view = view;
        this.isShowDialog = isShowDialog;
    }

    @Override
    protected void onStart() {
        if (view != null && isShowDialog) {
            view.showLoading();
        }
    }

    @Override
    public void onNext(T o) {
        onSuccess(o);
    }

    @Override
    public void onError(Throwable e) {
        if (view != null && isShowDialog) {
            view.hideLoading();
        }
        BaseException be = null;

        if (e != null) {

            if (e instanceof BaseException) {
                be = (BaseException) e;

                //回调到view层 处理 或者根据项目情况处理
                if (view != null) {
                    view.onErrorCode(new BaseModel(be.getErrorCode(), be.getErrorMsg()));
                } else {
                    onError(be.getErrorMsg());
                }

            } else {
                if (e instanceof HttpException) {
                    //   HTTP错误
                    be = new BaseException(BaseException.BAD_NETWORK_MSG, e, BaseException.BAD_NETWORK);
                } else if (e instanceof ConnectException
                        || e instanceof UnknownHostException) {
                    //   连接错误
                    be = new BaseException(BaseException.CONNECT_ERROR_MSG, e, BaseException.CONNECT_ERROR);
                } else if (e instanceof InterruptedIOException) {
                    //  连接超时
                    be = new BaseException(BaseException.CONNECT_TIMEOUT_MSG, e, BaseException.CONNECT_TIMEOUT);
                } else if (e instanceof JsonParseException
                        || e instanceof JSONException
                        || e instanceof ParseException) {
                    //  解析错误
                    be = new BaseException(BaseException.PARSE_ERROR_MSG, e, BaseException.PARSE_ERROR);
                } else {
                    be = new BaseException(BaseException.OTHER_MSG, e, BaseException.OTHER);
                }
            }
        } else {
            be = new BaseException(BaseException.OTHER_MSG, e, BaseException.OTHER);
        }

        onError(be.getErrorMsg());

    }

    @Override
    public void onComplete() {
        if (view != null && isShowDialog) {
            view.hideLoading();
        }

    }


    public abstract void onSuccess(T o);

    public abstract void onError(String msg);


}

ApiRetrofit中添加我们自定义的ConverterFactory

 .addConverterFactory(BaseConverterFactory.create())

这样的话,ApiServer便可以这样定义了

 @FormUrlEncoded
    @POST("/mapi/index.php?ctl=user&act=userbaseinfo")
    Observable<UserModel> getUserInfo(@FieldMap Map<String, String> params);

相应的,presenter可以这样写

  public void getUserInfo(Map<String, String> params) {
        addDisposable(apiServer.getUserInfo(params), new BaseObserver<UserModel>(baseView) {

            @Override
            public void onSuccess(UserModel o) {
                baseView.onGetUserInfoSucc(o);

            }

            @Override
            public void onError(String msg) {
                baseView.showError(msg);
            }
        });

    }

是不是精简了许多,快来试试吧

参考
RxJava2 + Retrofit2 完全指南 之 统一状态码/Exception处理

最后,献上源码 Github

你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢

上一篇下一篇

猜你喜欢

热点阅读