美文网首页Android开发经验谈Android开发
网络编程-Retrofit库源码分析

网络编程-Retrofit库源码分析

作者: 812cdf61e052 | 来源:发表于2020-08-04 10:02 被阅读0次

    Android核心知识点笔记github:https://github.com/AndroidCot/Android

    1、简介

    它的实质并不是网络请求,而只是对Okhttp进行了封装,让使用Okhttp更简单;完成网络请求参数的收集工作,后续由okhttp来搞定;但不能因为其没有进行网络请求,而忽略它的魅力,主要有几个方面的魅力:

    1. 高内聚低耦合,库设计的艺术很高
    2. 配置不同HttpClient来实现网络请求
    3. 支持同步、异步操作
    4. 兼容性强,可以兼容使用Rxjava、不同json解析库
    5. 使用非常方便灵活

    最常见依赖

     implementation 'com.squareup.retrofit2:retrofit:x.x.x'
     implementation 'com.squareup.retrofit2:converter-gson:x.x.x'
     implementation 'com.google.code.gson:gson:x.x.x'
     implementation 'com.squareup.retrofit2:adapter-rxjava:x.x.x'
    

    如果不使用Rxjava,adapter-rxjava依赖可以去掉;如果不使用gson库,可以替换掉com.google.code.gson依赖

    2、脉络

    1. 通过动态代理完成接口调用
    2. 变换操作均使用了抽象工厂模式、适配器/桥接模式
    3. CallAdapter 执行请求适配器,可以由开发者自己提供,但一般默认是OkHttpClient对象
    4. Converter主要是解决 json <---> object 之间的转换(http报文中的body内容处理)
    5. ParameterHandler 完成大部分注解转换

    3、源码细节

    3.1 初始化准备

    准备细节基本都集中再Retrofit类中,采用构造者模式,由内部类Builder处理

    3.1.1 回调线程池callbackExecutor

    使用内部类Builder.callbackExecutor方法添加

        public Builder callbackExecutor(Executor executor) {
          this.callbackExecutor = Objects.requireNonNull(executor, "executor == null");
          return this;
        }
    

    3.1.2 域名信息baseUrl

    使用内部类Builder.baseUrl方法添加

        public Builder baseUrl(URL baseUrl) {
          Objects.requireNonNull(baseUrl, "baseUrl == null");
          return baseUrl(HttpUrl.get(baseUrl.toString()));
        }
    

    3.1.3 http请求执行者callFactory

       public Builder client(OkHttpClient client) {
          return callFactory(Objects.requireNonNull(client, "client == null"));
        }
    
        public Builder callFactory(okhttp3.Call.Factory factory) {
          this.callFactory = Objects.requireNonNull(factory, "factory == null");
          return this;
        }
    

    3.1.4 http报文body处理者

    添加自定义,处理json ---- object

       public Builder addConverterFactory(Converter.Factory factory) {
          converterFactories.add(Objects.requireNonNull(factory, "factory == null"));
          return this;
        }
    

    添加库中默认处理者,处理Optional,stream, void, unit类型数据

        converterFactories.add(new BuiltInConverters());
          converterFactories.addAll(this.converterFactories);
          converterFactories.addAll(platform.defaultConverterFactories());
    

    Converter获取

    分为请求和结果两种;converterFactories中每个工厂,根据注解,如果生产Converter类,则返回

      public <T> Converter<T, RequestBody> requestBodyConverter(
          Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations) {
        return nextRequestBodyConverter(null, type, parameterAnnotations, methodAnnotations);
      }
    
      public <T> Converter<T, RequestBody> nextRequestBodyConverter(
          @Nullable Converter.Factory skipPast,
          Type type,
          Annotation[] parameterAnnotations,
          Annotation[] methodAnnotations) {
        Objects.requireNonNull(type, "type == null");
        Objects.requireNonNull(parameterAnnotations, "parameterAnnotations == null");
        Objects.requireNonNull(methodAnnotations, "methodAnnotations == null");
    
        int start = converterFactories.indexOf(skipPast) + 1;
        for (int i = start, count = converterFactories.size(); i < count; i++) {
          Converter.Factory factory = converterFactories.get(i);
          Converter<?, RequestBody> converter =
              factory.requestBodyConverter(type, parameterAnnotations, methodAnnotations, this);
          if (converter != null) {
            //noinspection unchecked
            return (Converter<T, RequestBody>) converter;
          }
        }
    
        。。。
      }
    
      public <T> Converter<ResponseBody, T> responseBodyConverter(Type type, Annotation[] annotations) {
        return nextResponseBodyConverter(null, type, annotations);
      }
    
      public <T> Converter<ResponseBody, T> nextResponseBodyConverter(
          @Nullable Converter.Factory skipPast, Type type, Annotation[] annotations) {
        Objects.requireNonNull(type, "type == null");
        Objects.requireNonNull(annotations, "annotations == null");
    
        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) {
            //noinspection unchecked
            return (Converter<ResponseBody, T>) converter;
          }
        }
    
       。。。
      }
    

    3.1.5 回调线程适配

    主要处理okhttp请求结果回调执行线程问题,同样可以添加自定义,也有默认处理

        public Builder addCallAdapterFactory(CallAdapter.Factory factory) {
          callAdapterFactories.add(Objects.requireNonNull(factory, "factory == null"));
          return this;
        }
        -------------------------------------------------------------------------------
          List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
          callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor));
    

    默认有两个DefaultCallAdapterFactory, 如果android sdk >=24,还会有CompletableFutureCallAdapterFactory;

    CallAdapter获取

    callAdapterFactories中每个工厂类,通过注解获取可以处理的适配器,如果不为空则找到;

      public CallAdapter<?, ?> callAdapter(Type returnType, Annotation[] annotations) {
        return nextCallAdapter(null, returnType, annotations);
      }
    
      public CallAdapter<?, ?> nextCallAdapter(
          @Nullable CallAdapter.Factory skipPast, Type returnType, Annotation[] annotations) {
        Objects.requireNonNull(returnType, "returnType == null");
        Objects.requireNonNull(annotations, "annotations == null");
    
        int start = callAdapterFactories.indexOf(skipPast) + 1;
        for (int i = start, count = callAdapterFactories.size(); i < count; i++) {
          CallAdapter<?, ?> adapter = callAdapterFactories.get(i).get(returnType, annotations, this);
          if (adapter != null) {
            return adapter;
          }
        }
    
        。。。
      }
    

    3.2 执行过程

    介绍下异步过程,通过过程在请求方法上存在区别,流程处理基本一致

    3.2.1 动态代理

    public <T> T create(final Class<T> service) {
        validateServiceInterface(service);
        return (T)
            Proxy.newProxyInstance(
                service.getClassLoader(),
                new Class<?>[] {service},
                new InvocationHandler() {
                  private final Platform platform = Platform.get();
                  private final Object[] emptyArgs = new Object[0];
    
                  @Override
                  public @Nullable Object invoke(Object proxy, Method method, @Nullable Object[] args)
                      throws Throwable {
                    if (method.getDeclaringClass() == Object.class) {
                      return method.invoke(this, args);
                    }
                    args = args != null ? args : emptyArgs;
                    return platform.isDefaultMethod(method)
                        ? platform.invokeDefaultMethod(method, service, proxy, args)
                        : loadServiceMethod(method).invoke(args);
                  }
                });
      }
    

    分两种情况

    1. 接口有默认实现,使用Lookup、MethodHandles的反射技术,来实现方法调用;这种反射技术需要知道调用类信息,方法、字段信息,才可以反射调用; 没有用过这种思路
    2. 通过ServiceMethod.invoke来构造返回方法结果,这个是Okhttp中可请求的参数结果

    3.2.2 ServiceMethod 构造请求

    其抽象实现类HttpServiceMethod, 具体实现类CallAdapted,SuspendForResponse,SuspendForBody;抽象方法为

    protected abstract @Nullable ReturnT adapt(Call<ResponseT> call, Object[] args);
    

    主要是再invoke方法中调用;其它方法实现了CallAdapter的获取,respondBody 的Converter的获取,也是通过Retrofit代理获取的

      final @Nullable ReturnT invoke(Object[] args) {
        Call<ResponseT> call = new OkHttpCall<>(requestFactory, args, callFactory, responseConverter);
        return adapt(call, args);
      }
    

    serviceMethodCache用来缓存Method信息、方法注解信息、方法参数注解信息;parseAnnotations方法就是来解析注解信息的

    ServiceMethod<?> loadServiceMethod(Method method) {
        ServiceMethod<?> result = serviceMethodCache.get(method);
        if (result != null) return result;
    
        synchronized (serviceMethodCache) {
          result = serviceMethodCache.get(method);
          if (result == null) {
            result = ServiceMethod.parseAnnotations(this, method);
            serviceMethodCache.put(method, result);
          }
        }
        return result;
      }
    

    3.2.3 注解解析过程

    请求信息收集

    RequestFactory的parseAnnotations方法进行收集;方法注解直接收集并解析;参数注解通过ParameterHandler子类+参数值来实现解析,create方法即为完成参数注解解析过程

    callAdapter、Converter<ResponseBody, ResponseT>收集

    HttpServiceMethod类中HttpServiceMethod方法来实现

    3.2.4 开始异步请求

    invoke方法执行开始请求,其内部由HttpServiceMethod 子类中通过adapt方法,这个方法进而调用callAdapter方法

       final @Nullable ReturnT invoke(Object[] args) {
        Call<ResponseT> call = new OkHttpCall<>(requestFactory, args, callFactory, responseConverter);
        return adapt(call, args);
      }
    --------------------------------------------------------------------------------------------------
    callAdapter.adapt(call)
    

    callAdapter其实是对OkhttpCall回调方法进行了包装;

    1. 不进行任何包装
    2. 包装为DefaultCallAdapterFactory.ExecutorCallbackCall,如果提供了线程池,则回调在线程池中执行
    3. 包装为CompletableFutureCallAdapterFactory.ResponseCallback,异步执行

    3.3 异步结果处理

    主要在OkHttpCall中进行

    public void enqueue(final Callback<T> callback) {
        Objects.requireNonNull(callback, "callback == null");
    
        okhttp3.Call call;
        Throwable failure;
    
        synchronized (this) {
          if (executed) throw new IllegalStateException("Already executed.");
          executed = true;
    
          call = rawCall;
          failure = creationFailure;
          if (call == null && failure == null) {
            try {
              call = rawCall = createRawCall();
            } catch (Throwable t) {
              throwIfFatal(t);
              failure = creationFailure = t;
            }
          }
        }
    
        if (failure != null) {
          callback.onFailure(this, failure);
          return;
        }
    
        if (canceled) {
          call.cancel();
        }
    
        call.enqueue(
            new okhttp3.Callback() {
              @Override
              public void onResponse(okhttp3.Call call, okhttp3.Response rawResponse) {
                Response<T> response;
                try {
                  response = parseResponse(rawResponse);
                } catch (Throwable e) {
                  throwIfFatal(e);
                  callFailure(e);
                  return;
                }
    
                try {
                  callback.onResponse(OkHttpCall.this, response);
                } catch (Throwable t) {
                  throwIfFatal(t);
                  t.printStackTrace(); // TODO this is not great
                }
              }
    
              @Override
              public void onFailure(okhttp3.Call call, IOException e) {
                callFailure(e);
              }
    
              private void callFailure(Throwable e) {
                try {
                  callback.onFailure(OkHttpCall.this, e);
                } catch (Throwable t) {
                  throwIfFatal(t);
                  t.printStackTrace();
                }
              }
            });
      }
    
    1. 通过createRawCall方法,创建真正的请求对象okhttp3.Call
    2. 通过okhttp3.Call 的enquen,来进行异步请求
    3. 通过parseResponse方法对okhttp请求结果解析,并回调
    • 若是code码 >= 300或者 <200,则请求失败
    • 若是204、205,则无返回内容
    • 使用responseConverter 进行内容转换

    4、结语

    总的来说,设计模式使用灵活精巧,反射、注解信手拈来;介绍时,却很http注解处理的很多细节都没有写进来,这些细节会在后面关于http报文内容,单独来写

    1. 使用了大量了的注解和反射,使用更方便
    2. 使用动态代理,十分解耦
    3. 使用了适配、桥接模式使注释处理、结果处理更轻松

    技术变化都很快,但基础技术、理论知识永远都是那些;作者希望在余后的生活中,对常用技术点进行基础知识分享;如果你觉得文章写的不错,请给与关注和点赞;如果文章存在错误,也请多多指教!

    相关文章

      网友评论

        本文标题:网络编程-Retrofit库源码分析

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