Retrofit源码学习笔记

2019-05-13  本文已影响0人  Zwww_

最近看了Retrofit(2.5.0)的部分源码,借此博客记录下自己对Retrofit的理解,功力善浅,如有错误欢迎各位大佬指正。

1、简单使用Retrofit进行异步请求

image.png
image.png

Retrofit的请求分为四步

2、Retorfit类分析

采用了构建者模式来构建,避免在构造方法传入过多参数导致使用者混淆。

先分析Retrofit的内部类Builder

Builder成员变量
  public static final class Builder {
    private final Platform platform;
    private @Nullable okhttp3.Call.Factory callFactory;
    private @Nullable HttpUrl baseUrl;
    private final List<Converter.Factory> converterFactories = new ArrayList<>();
    private final List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>();
    private @Nullable Executor callbackExecutor;
    private boolean validateEagerly;
    ......省略
    }

这里只贴出了Builder类的成员变量,可以看出这个类的成员变量不多,也就是说Retrofit的配置并不像okhttp那么多

Builder构造方法
  Builder(Platform platform) {
      this.platform = platform;
    }

    public Builder() {
      this(Platform.get());
    }

    Builder(Retrofit retrofit) {
      platform = Platform.get();
      callFactory = retrofit.callFactory;
      baseUrl = retrofit.baseUrl;

      // Do not add the default BuiltIntConverters and platform-aware converters added by build().
      for (int i = 1,
          size = retrofit.converterFactories.size() - platform.defaultConverterFactoriesSize();
          i < size; i++) {
        converterFactories.add(retrofit.converterFactories.get(i));
      }

      // Do not add the default, platform-aware call adapters added by build().
      for (int i = 0,
          size = retrofit.callAdapterFactories.size() - platform.defaultCallAdapterFactoriesSize();
          i < size; i++) {
        callAdapterFactories.add(retrofit.callAdapterFactories.get(i));
      }

      callbackExecutor = retrofit.callbackExecutor;
      validateEagerly = retrofit.validateEagerly;
    }
Builder的build方法
 public Retrofit build() {
      if (baseUrl == null) {
        throw new IllegalStateException("Base URL required.");
      }

      okhttp3.Call.Factory callFactory = this.callFactory;
      if (callFactory == null) {
        callFactory = new OkHttpClient();
      }

      Executor callbackExecutor = this.callbackExecutor;
      if (callbackExecutor == null) {
        callbackExecutor = platform.defaultCallbackExecutor();
      }

      // Make a defensive copy of the adapters and add the default Call adapter.
      List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
      callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor));

      // Make a defensive copy of the converters.
      List<Converter.Factory> converterFactories = new ArrayList<>(
          1 + this.converterFactories.size() + platform.defaultConverterFactoriesSize());

      // Add the built-in converter factory first. This prevents overriding its behavior but also
      // ensures correct behavior when using converters that consume all types.
      converterFactories.add(new BuiltInConverters());
      converterFactories.addAll(this.converterFactories);
      converterFactories.addAll(platform.defaultConverterFactories());

      return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
          unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
    }

接着分析Retrofit

Retrofit的成员变量

Retrofit的成员变量和Retrofit的内部类Builder里面的成员变量是基本一致的,这里就不做阐述了。

Retrofit的create方法

我们前面总结的Retrofit简单异步请求的第三步就是通过调用Retrofit的creat方法来创建RetrofitInterface接口实例。我们现在来看下内部是如何工作的。


image.png

3、HttpServiceMethod类

从上面我们知道了Retrofit的Creat方法创建了一个动态代理,当我们调用请求接口方法时,代理的invoke方法便会执行,这时候主要是通过HttpServiceMethod来解析请求接口(RetrofitInterface)中定义的网络请求方法。所以,我这里想多多认识HttpServiceMethod。

基类ServiceMethod

前面我们提到的Retrofit的loadServiceMethod方法中,用serviceMethodCache来缓存Method实例和对应的HttpServiceMethod实例,而这个HttpServiceMethod实例是通过ServiceMethod的parseAnnotations方法获得的。


image.png

parseAnnotations方法首先构建一个RequestFactory实例


image.png
这里同样是采用了构建者模式
image.png

获取了method的注解,形参类型,形参注解。显而易见,RequestFactory类就是将我们在RetroInteface接口中定义的请求方法注解,返回值,参数类型等等进行解析并存储。我们接着看ServiceMethod的parseAnnotations方法,接下来是判断method的返回值是否合法,不能包括可变类型或者通配符,并且不能为空,否则抛出异常,也就是说我们的网络请求方法的返回值不能包括可变类型或者通配符,并且不能为空。接着继续调用子类HttpServiceMethod的parseAnnotations方法

HttpServiceMethod的parseAnnotations方法
image.png

这里34行就是获取我们添加到Retorfit成员变量callAdapterFactories中的RxJava2CallAdapterFactory实例然后来创建一个RxJava2CallAdapter实例返回。接下来就是处理method的返回值类型,再通过我们添加到Retorfit成员变量converterFactories中的GsonConverterFactory实例来创建一个GsonResponseBodyConverter实例返回。最后再通过RequestFactory实例,okhttp3.Call.Factory实例,RxJava2CallAdapter实例和GsonResponseBodyConverter实例来构建一个HttpServiceMethod实例。
由上面的这些内容我们可以明白HttpServiceMethod这个类的主要功能是通过RxJava2CallAdapter将得到的RequestFactory,okhttp3.Call.Factory,GsonResponseBodyConverter实例创建一个OkHttpCall实例。

4、总结

上一篇下一篇

猜你喜欢

热点阅读