Retrofit源码解析

作者: 小村医 | 来源:发表于2019-09-22 21:55 被阅读0次

应用程序通过Retrofit请求网络,实际上是使用Retrofit接口封装请求参数,之后由OkHttp完成后续的操作

在服务端返回收据后,OkHttp将原始的结果交给Retrofit,Retrofit根据用户的需求对结果进行解析

一、 Retrofit使用

  1. 定义网络请求接口,并为接口中的方法添加注解
public interface GitHubService {
    @GET("users/{user}/repos")
    Call<List<Repo>> listRepos(@Path("user") String user);
}
  1. 创建Retrofit实例
Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
  1. 创建网络请求接口实例
GitHubService service = retrofit.create(GitHubService.class);
  1. 发送网络请求
  Call repos = (Call) service.listRepos("XXXX");
        repos.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }

        });

二、Retrofit执行流程

  1. 创建Retrofit实例
  2. 定义一个网络请求接口并未接口中的方法添加注解
  3. 通过动态代理生成网络请求对象
  4. 通过网络请求适配器将网络请求对象进行平台适配
  5. 通过网络请求执行器发送网络请求
  6. 通过数据转换器解析数据
  7. 通过回调执行器切换线程
  8. 用户在主线程处理返回结果

三、创建Retrofit实例

 Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://api.github.com/") // 设置网络请求的Url地址
                .addConverterFactory(GsonConverterFactory.create()) // 设置数据解析器
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 支持RxJava平台
                .build();

使用Builder模式创建一个Retrofit实例,下面看一下Retrofit里几个比较重要的成员变量:

  • Map<Method, ServiceMethod> serviceMethodCache: key值是请求方法,value是网络请求接口中对注解解析后生成的对象
  • okhttp3.Call.Factory callFactory:请求网络的OkHttp的工厂
  • HttpUrl baseUrl:网络请求的基地址
  • List<Converter.Factory> converterFactories:数据转换器工厂的集合
  • List<CallAdapter.Factory> adapterFactories:网络请求适配器工厂的集合
  • Executor callbackExecutor:用于执行回调
  • boolean validateEagerly:是否需要立即解析接口中的方法

Retrofit.Builder

  public static final class Builder {
    private Platform platform;
    public Builder() {
      this(Platform.get());
    }

    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;
      // 根据不同的平台设置不同的callback执行器
      if (callbackExecutor == null) {
        callbackExecutor = platform.defaultCallbackExecutor();
      }

      List<CallAdapter.Factory> adapterFactories = new ArrayList<>(this.adapterFactories);
      adapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));
      List<Converter.Factory> converterFactories = new ArrayList<>(this.converterFactories);

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

Platform

class Platform {
  private static final Platform PLATFORM = findPlatform();

  static Platform get() {
    return PLATFORM;
  }
  // 判断当前平台
  private static Platform findPlatform() {
    try {
      Class.forName("android.os.Build");
      if (Build.VERSION.SDK_INT != 0) {
        return new Android();
      }
    } catch (ClassNotFoundException ignored) {
    }
    try {
      Class.forName("java.util.Optional");
      return new Java8();
    } catch (ClassNotFoundException ignored) {
    }
    try {
      Class.forName("org.robovm.apple.foundation.NSObject");
      return new IOS();
    } catch (ClassNotFoundException ignored) {
    }
    return new Platform();
  }
}

  static class Android extends Platform {
    @Override public Executor defaultCallbackExecutor() {
      return new MainThreadExecutor();
    }
    // 创建默认的网络请求适配器工厂,主要作用是让call请求在异步调用的时候指定Executor执行器,让它来执行回调
    @Override CallAdapter.Factory defaultCallAdapterFactory(Executor callbackExecutor) {
      return new ExecutorCallAdapterFactory(callbackExecutor);
    }

    static class MainThreadExecutor implements Executor {
      // 通过handler做线程切换
      private final Handler handler = new Handler(Looper.getMainLooper());

      @Override public void execute(Runnable r) {
        handler.post(r);
      }
    }
  }

四、ExecutorCallAdapterFactory工作原理

  1. 初始化
static class Android extends Platform {
    @Override public Executor defaultCallbackExecutor() {
      return new MainThreadExecutor();
    }

    @Override CallAdapter.Factory defaultCallAdapterFactory(Executor callbackExecutor) {
      //持有回调给用户的执行器MainThreadExecutor
      return new ExecutorCallAdapterFactory(callbackExecutor);
    }

    static class MainThreadExecutor implements Executor {
      private final Handler handler = new Handler(Looper.getMainLooper());

      @Override public void execute(Runnable r) {
        handler.post(r);
      }
    }
  }
  1. get(),获取CallAdapter的方法
  @Override
  public CallAdapter<Call<?>> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    if (getRawType(returnType) != Call.class) {
      return null;
    }
    final Type responseType = Utils.getCallResponseType(returnType);
    return new CallAdapter<Call<?>>() {
      @Override public Type responseType() {
        return responseType;
      }
      //返回用户最终持有的网络请求的Call对象
      @Override public <R> Call<R> adapt(Call<R> call) {
        return new ExecutorCallbackCall<>(callbackExecutor, call);
      }
    };
  }

CallAdapter.adapt()方法会返回ExecutorCallbackCall对象,而此对象是用户最终得到的用于请求的Call

  1. ExecutorCallbackCall执行网络请求
    @Override public void enqueue(final Callback<T> callback) {
      if (callback == null) throw new NullPointerException("callback == null");

      delegate.enqueue(new Callback<T>() {
        @Override public void onResponse(Call<T> call, final Response<T> response) {
          // 回调执行器,让回调在主线程执行
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              if (delegate.isCanceled()) {
                callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
              } else {
                callback.onResponse(ExecutorCallbackCall.this, response);
              }
            }
          });
        }

        @Override public void onFailure(Call<T> call, final Throwable t) {
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              callback.onFailure(ExecutorCallbackCall.this, t);
            }
          });
        }
      });
    }

五、网络请求接口实例解析

GitHubService service = retrofit.create(GitHubService.class);

retrofit 通过create方法闯将一个网络请求接口的实例,下面来看一下create的实现

  public <T> T create(final Class<T> service) {
     // 对传进来的接口进行验证
    Utils.validateServiceInterface(service);
    // 根据标记位判断是否需要提前解析接口
    if (validateEagerly) {
      eagerlyValidateMethods(service);
    }
    // 通过动态代理创建接口实例
    return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
        new InvocationHandler() {
          private final Platform platform = Platform.get();

          @Override public Object invoke(Object proxy, Method method, Object... args)
              throws Throwable {
            ......
            //加载ServiceMethod:对应接口中的一个网络请求方法 
            ServiceMethod serviceMethod = loadServiceMethod(method);
            // 实际的网络请求,OkHttpCall是对OkHttp的封装
            OkHttpCall okHttpCall = new OkHttpCall<>(serviceMethod, args);
            return serviceMethod.callAdapter.adapt(okHttpCall);
          }
        });
  }

看一下提前接口方法里做了什么操作

  private void eagerlyValidateMethods(Class<?> service) {
    Platform platform = Platform.get();
    // 遍历接口中的方法
    for (Method method : service.getDeclaredMethods()) {
      if (!platform.isDefaultMethod(method)) {
        // 开始真正的解析
        loadServiceMethod(method);
      }
    }
  }

loadServiceMethod会把接口中的方法解析成一个对应的ServiceMethod,ServiceMethod是对网络请求的一个封装

  ServiceMethod loadServiceMethod(Method method) {
    ServiceMethod result;
    synchronized (serviceMethodCache) {
      // 缓存中有直接使用
      result = serviceMethodCache.get(method);
      if (result == null) {
        // 使用ServiceMethod.Builder解析并创建一个ServiceMethod
        result = new ServiceMethod.Builder(this, method).build();
        // 解析完成后把结果放到缓存中
        serviceMethodCache.put(method, result);
      }
    }
    return result;
  }

ServiceMethod对象解析

final class ServiceMethod<T> {
  // Upper and lower characters, digits, underscores, and hyphens, starting with a character.
  static final String PARAM = "[a-zA-Z][a-zA-Z0-9_-]*";
  static final Pattern PARAM_URL_REGEX = Pattern.compile("\\{(" + PARAM + ")\\}");
  static final Pattern PARAM_NAME_REGEX = Pattern.compile(PARAM);
  // 网络请求工厂,使用OkHttp生产网络请求(Call)
  final okhttp3.Call.Factory callFactory;
  // 网络请求适配使上面生产的call能适用于各个平台,如rxjava
  final CallAdapter<?> callAdapter;
   // 基地址
  private final HttpUrl baseUrl;
   //  数据转换器 ,把服务端返回的数据转换成我们需要的javabean对象
  private final Converter<ResponseBody, T> responseConverter;
   // http请求方式:get,post等
  private final String httpMethod;
   // 网络请求的相对地址
  private final String relativeUrl;
   // 网络请求的headers
  private final Headers headers;
  private final MediaType contentType;
  private final boolean hasBody;
  private final boolean isFormEncoded;
  private final boolean isMultipart;
   // 方法参数的处理器 ,方法中的参数和注解都是通过它来解析的
  private final ParameterHandler<?>[] parameterHandlers;
  static final class Builder<T> {
    public Builder(Retrofit retrofit, Method method) {
      this.retrofit = retrofit;
      this.method = method;
      //获取方法的注解
      this.methodAnnotations = method.getAnnotations();
      //获取方法的参数的类型
      this.parameterTypes = method.getGenericParameterTypes();
      //获取方法的参数注解的内容
      this.parameterAnnotationsArray = method.getParameterAnnotations();
    }
    //ServiceMethod具体的创建和解析 
    public ServiceMethod build() {
      // 调用createCallAdapter完成callAdapter的初始化
      callAdapter = createCallAdapter();
      responseType = callAdapter.responseType();
       // 创建数据转换器
      responseConverter = createResponseConverter();
       //  遍历方法的注解
      for (Annotation annotation : methodAnnotations) {
        // 解析方法中的注解
        parseMethodAnnotation(annotation);
      }


      int parameterCount = parameterAnnotationsArray.length;
      parameterHandlers = new ParameterHandler<?>[parameterCount];
      for (int p = 0; p < parameterCount; p++) {
        Type parameterType = parameterTypes[p];
   
        Annotation[] parameterAnnotations = parameterAnnotationsArray[p];
        if (parameterAnnotations == null) {
          throw parameterError(p, "No Retrofit annotation found.");
        }
        //解析方法中的参数 
        parameterHandlers[p] = parseParameter(p, parameterType, parameterAnnotations);
      }
      return new ServiceMethod<>(this);
    }

createCallAdapter :根据网络请求接口的返回值和注解的类型获取对应的网络接口适配器

    private CallAdapter<?> createCallAdapter() {
      // 获取返回类型
      Type returnType = method.getGenericReturnType();
      // 获取注解
      Annotation[] annotations = method.getAnnotations();
      try {
        return retrofit.callAdapter(returnType, annotations);
      } catch (RuntimeException e) {
        throw methodError(e, "Unable to create call adapter for %s", returnType);
      }
    }

Retrofit:
  public CallAdapter<?> callAdapter(Type returnType, Annotation[] annotations) {
    return nextCallAdapter(null, returnType, annotations);
  }
  public CallAdapter<?> nextCallAdapter(CallAdapter.Factory skipPast, Type returnType,
      Annotation[] annotations) {

    int start = adapterFactories.indexOf(skipPast) + 1;
    // 遍历callAdapter的工厂集合 寻找合适的工厂
    for (int i = start, count = adapterFactories.size(); i < count; i++) {
      // 通过工厂的get方法创建CallAdapter
      CallAdapter<?> adapter = adapterFactories.get(i).get(returnType, annotations, this);
      if (adapter != null) {
        return adapter;
      }
    }

    StringBuilder builder = new StringBuilder("Could not locate call adapter for ")
        .append(returnType)
        .append(".\n");
    throw new IllegalArgumentException(builder.toString());
  }

createResponseConverter :创建数据转换器

    private Converter<ResponseBody, T> createResponseConverter() {
      // 获取方法的注解
      Annotation[] annotations = method.getAnnotations();
      try {
        return retrofit.responseBodyConverter(responseType, annotations);
      } catch (RuntimeException e) { 
      }
    }


Retrofit:
  public <T> Converter<ResponseBody, T> responseBodyConverter(Type type, Annotation[] annotations) {
    return nextResponseBodyConverter(null, type, annotations);
  }

  public <T> Converter<ResponseBody, T> nextResponseBodyConverter(Converter.Factory skipPast,
      Type type, Annotation[] annotations) {
 
    int start = converterFactories.indexOf(skipPast) + 1;
    // 遍历数据转换器工厂集合,找到合适的数据转换器工厂
    for (int i = start, count = converterFactories.size(); i < count; i++) {
      Converter<ResponseBody, ?> converter =
          converterFactories.get(i).responseBodyConverter(type, annotations, this);
      if (converter != null) {
        return (Converter<ResponseBody, T>) converter;
      }
    }

    StringBuilder builder = new StringBuilder("Could not locate ResponseBody converter for ")
        .append(type)
        .append(".\n");
    throw new IllegalArgumentException(builder.toString());
  }

六、OkHttpCall对象

final class OkHttpCall<T> implements Call<T> {
  // 网络请求参数的封装
  private final ServiceMethod<T> serviceMethod;
  private final Object[] args;

  private volatile boolean canceled;

  // 真正的网络请求
  private okhttp3.Call rawCall;
  private Throwable creationFailure; 
  private boolean executed;
}

相关文章

网友评论

    本文标题:Retrofit源码解析

    本文链接:https://www.haomeiwen.com/subject/dkiyuctx.html