RxHttp 让你眼前一亮的Http请求框架( 八 )

4.3、HttpSenderHttpSender可以把它理解为请求发送者,里面声明OkHttpClient对象和一系列静态方法,我们来简单看下:
public final class HttpSender {private static OkHttpClient mOkHttpClient; //只能初始化一次,第二次将抛出异常//处理化OkHttpClient对象public static void init(OkHttpClient okHttpClient) {if (mOkHttpClient != null)throw new IllegalArgumentException("OkHttpClient can only be initialized once");mOkHttpClient = okHttpClient;}//通过Param对象同步执行一个请求public static Response execute(@NonNull Param param) throws IOException {return newCall(param).execute();}static Call newCall(Param param) throws IOException {return newCall(getOkHttpClient(), param);}//所有的请求,最终都会调此方法拿到Call对象,然后执行请求static Call newCall(OkHttpClient client, Param param) throws IOException {param = RxHttpPlugins.onParamAssembly(param);if (param instanceof IUploadLengthLimit) {((IUploadLengthLimit) param).checkLength();}Request request = param.buildRequest();//通过Param拿到Request对象LogUtil.log(request);return client.newCall(request);}//省略了部分方法}这里我们重点看下newCall(OkHttpClient, Param)方法,该方法第一行就是为Param添加公共参数;然后判断Param有没有实现IUploadLengthLimit接口,有的话,检查文件上传大小,超出大小,则抛出IO异常;接着就是通过Param拿到Request对象;最后拿到Call对象,就可以发送一个请求 。
4.4、Parser先看下Parser继承结构图

RxHttp 让你眼前一亮的Http请求框架

文章插图
 
这里对上图中的类做个简单的介绍
  • Parser:接口类,里面定义了一个T onParse(Response)方法,输入Response对象,输出实体类对象T
  • AbstractParser:抽象类,里面没有任何具体实现,主要作用是在构造方法内获取泛型类型
  • SimpleParser:是一个万能的解析器,可以解析任意数据结构,RxHttp内置的大部分asXxx方法,内部就是通过该解析器实现的
  • ListParser:是一个列表解析器,可以解析任意列表数据,内置asList(Class<T>)方法,就是通过该解析器实现的
  • MapParser:是一个Map解析器,可以解析任意Map数据类型,内置的asMap系列方法,就是通过该解析器实现的
  • BitmapParser:是一个Bitmap解析器,通过该解析器可以获得一个Bitmap对象,asBitmap()方法内部就是通过该解析器实现的
  • DownloadParser:文件下载解析器,用于文件下载,内置的一系列asDownload方法就是通过该解析器实现的
5、扩展5.1、自定义Parser前面第二部曲中,我们介绍了一系列asXxx方法,通过该系列方法可以很方便的指定数据返回类型,特别是自定义的asResponse(Class<T>)、asResponseList(Class<T>)、asResponsePageList(Class<T>)这3个方法,将Reponse<T>类型数据,处理的简直不要太完美,下面我们就来看看如何自定义Parser 。
源码永远是最好的学习方式,在学习自定义Parser前,我们不妨先看看内置的Parser是如何实现的
SimPleParser
public class SimpleParser<T> extends AbstractParser<T> {//省略构造方法@Overridepublic T onParse(Response response) throws IOException {return convert(response, mType);}}可以看到,SimpleParser除了构造方法,就剩一个onParser方法,该方法是在Parser接口中定义的,再来看看具体的实现convert(Response, Type),这个方法也是在Parser接口中定义的,并且有默认的实现,如下:
public interface Parser<T> {//输入Response 输出TT onParse(@NonNull Response response) throws IOException;//对Http返回的结果,转换成我们期望的实体类对象default <R> R convert(Response response, Type type) throws IOException {ResponseBody body = ExceptionHelper.throwIfFatal(response);//这里内部会判断code<200||code>=300 时,抛出异常boolean onResultDecoder = isOnResultDecoder(response); //是否需要对返回的数据进行解密LogUtil.log(response, onResultDecoder, null);IConverter converter = getConverter(response);//取出转换器return converter.convert(body, type, onResultDecoder); //对数据进场转换}//省略若干方法}可以看到,非常的简单,输入Response对象和泛型类型Type,内部就通过IConverter接口转换为我们期望的实体类对象并返回 。
到这,我想大家应该就多少有点明白了,自定义Parser,无非就是继承AbstractParser,然后实现onParser方法即可,那我们来验证一下,我们来看看内置ListParser是不是这样实现的,如下:
public class ListParser<T> extends AbstractParser<List<T>> {//省略构造方法@Overridepublic List<T> onParse(Response response) throws IOException {final Type type = ParameterizedTypeImpl.get(List.class, mType); //拿到泛型类型return convert(response, type);}}


推荐阅读