可以看到,跟SimpleParser解析器几乎是一样的实现,不同是的,这里将我们输入的泛型T与List组拼为一个新的泛型类型,最终返回List<T>对象 。
现在,我们就可以来自定义Parser了,先来自定义ResponseParser,用来处理Response<T>数据类型,先看看数据结构:
public class Response<T> {private intcode;private String msg;private Tdata;//这里省略get、set方法}自定义ResponseParser代码如下:
//通过@Parser注解,为解析器取别名为Response,此时就会在RxHttp类生成asResponse(Class<T>)方法@Parser(name = "Response") public class ResponseParser<T> extends AbstractParser<T> {//省略构造方法@Overridepublic T onParse(okhttp3.Response response) throws IOException {final Type type = ParameterizedTypeImpl.get(Response.class, mType); //获取泛型类型Response<T> data = https://www.isolves.com/it/cxkf/kj/2019-12-27/convert(response, type);T t = data.getData(); //获取data字段if (data.getCode() != 0 || t == null) {//这里假设code不等于0,代表数据不正确,抛出异常throw new ParseException(String.valueOf(data.getCode()), data.getMsg(), response);}return t;}}可以看到,非常的简单,首先将我们输入泛型和自定义的Response<T>类组拼成新的泛型类型,随后通过convert(Response, Type)方法得到Response<T>对象,接着又对code及T做了判断,如果不正确就抛出异常,最后返回T 。
估计这里有人会问,我怎么用这个解析器呢?相信不少小伙伴以及发现了,我们在ResponseParser类名上面用了@Parser注解,只要用了该注解,就会在RxHttp自动生成asXxx(Class<T>)方法,其中Xxx就是我们在@Parser注解中为解析器取的别名,这里取别名为Response,所以便会在RxHttp类中自动生成asResponse(Class<T>)方法,如下:
public <T> Observable<T> asResponse(Class<T> type) {return asParser(new ResponseParser(type));}可以看到,该方法内部又调用了asParser(Parser<T>)方法,并传入了ResponseParser,因此,我们有两种方式使用自定义的ResponseParser,如下:
//第一种方式,使用@parser注解生成的asResponse方法RxHttp.postForm("/service/...")//发送post表单请求.add("key", "value")//添加参数,可调用多次.asResponse(Student.class)//返回Student类型.subscribe(student -> {//请求成功,这里能拿到 Student对象}, throwable -> {//请求失败});//第二种方式,直接使用asParser(Parser<T>)方法RxHttp.postForm("/service/...")//发送post表单请求.add("key", "value")//添加参数,可调用多次.asParser(new ResponseParser<Student>(){})//返回Student类型.subscribe(student -> {//请求成功,这里能拿到 Student对象}, throwable -> {//请求失败});以上两种方式,除了写法上的区别,其它都一样,用哪种,看个人喜好,但还是建议使用第一种方式,不仅写法简单,也降低了耦合 。
这里最后再贴上ResponseListParser、ResponsePageListParser的源码,原理都是一样的,代码实现也差不多,就不再详解 ResponseListParser
@Parser(name = "ResponseList")public class ResponseListParser<T> extends AbstractParser<List<T>> {//省略构造方法@Overridepublic List<T> onParse(okhttp3.Response response) throws IOException {final Type type = ParameterizedTypeImpl.get(Response.class, List.class, mType); //获取泛型类型Response<List<T>> data = https://www.isolves.com/it/cxkf/kj/2019-12-27/convert(response, type);ListResponsePageListParser
@Parser(name = "ResponsePageList")public class ResponsePageListParser<T> extends AbstractParser<PageList<T>> {//省略构造方法@Overridepublic PageList<T> onParse(okhttp3.Response response) throws IOException {final Type type = ParameterizedTypeImpl.get(Response.class, PageList.class, mType); //获取泛型类型Response<PageList<T>> data = https://www.isolves.com/it/cxkf/kj/2019-12-27/convert(response, type);PageList5.2、自定义Param自定义Param,想较于自定义Parser,要更加的简单,我们只需根据自己的需求,继承NoBodyParam、FormParam、JsonParam等,增加或者重写方法即可,比如我们有以下3种情况,需要自定义Param,如下:
推荐阅读
- 给你百万年薪,让你担任公司的架构师,你知道该做哪些事吗?
- 治眼疾茶方推荐
- 台灯什么角度最保护眼睛,写作业台灯的正确摆放位置
- 怎样看台灯伤不伤眼睛,台灯闪烁会伤眼睛吗
- 肚脐眼到底能不能抠?抠了会什么样的后果,你知道吗
- 索隆的眼睛怎么回事 索隆的一只眼睛怎么瞎的
- 10款自制减肥茶 让你轻松享瘦
- 阴阳眼可以看见什么 阴阳眼的人能看到什么
- 减辐射护眼睛 每天宜喝四杯茶
- 双眼皮疤痕增生的前兆有哪些
