Retroft 源码——请求接口流程

Retrofit使用教程地址 http://square.github.io/retrofit/
源码版本

com.squareup.retrofit2:retrofit:2.5.0

有时候看源码,似懂非懂的感觉。着实让人迷惑不堪,所以这次直接针对源码进行阅读,调试。看看retrofit是执行流程。

  1. 第一步,创建Retrofit对象,通过创建者模式配置Retrofit里面的一些参数(具体参数可看源码),然后进行实例。
  2. 第二步,通过Retrofit实例对象,调用create(ApiService.class)方法。此方法通过代理模式,解析ApiService接口类里面的方法,注解,参数。在其过程中并通过loadServiceMethod(method)将其方法,注解,参数缓存到private final Map<Method, ServiceMethod<?>> serviceMethodCache = new ConcurrentHashMap<>();当中。第二次进行加载时,直接从缓存当中取出,进行网络加载请求。
  3. 第三步,请求加载成功,针对后台返回的数据进行操作。

通过建造者模式,生成Retrofit实例

   
       /**
     * 创建Retrofit 实例,并针对参数进行配置
     * Create the {@link Retrofit} instance using the configured values.
     * <p>
     * Note: If neither {@link #client} nor {@link #callFactory} is called a default {@link
     * OkHttpClient} will be created and used.
     */
    public Retrofit build() {
        if (baseUrl == null) {
            throw new IllegalStateException("Base URL required.");
        }

        okhttp3.Call.Factory callFactory = this.callFactory;
        if (callFactory == null) {
            //实例OkhttpClient对象
            callFactory = new OkHttpClient();
        }

        Executor callbackExecutor = this.callbackExecutor;
        if (callbackExecutor == null) {
            callbackExecutor = platform.defaultCallbackExecutor();
        }
        //默认配置返回参数适配器,例如,返回RxJava2CallAdapterFactory
        // 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));
        //默认配置返回的数据类型,比如GSON(GsonConverterFactory),XML,
        // 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());
        //通过建造者模式,返回Retrofit实例
        return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
                unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
    }
    

通过调用create()方法请求网络


  public <T> T create(final Class<T> service) {
  //判断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();
          //存储方法里面的传参
          private final Object[] emptyArgs = new Object[0];
         //通过代理模式
          @Override public Object invoke(Object proxy, Method method, @Nullable Object[] args)
              throws Throwable {
            // If the method is a method from Object then defer to normal invocation.
            if (method.getDeclaringClass() == Object.class) {
              return method.invoke(this, args);
            }
            if (platform.isDefaultMethod(method)) {
              return platform.invokeDefaultMethod(method, service, proxy, args);
            }
            //通过该方法实现网络通讯
            return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
          }
        });
  }

上面可以看出,通过代理模式,将其ApiService.class里面的方法method,参数emptyArgs ,注解,通过loadServiceMethod(method)该方法进行解析。

    ServiceMethod<?> loadServiceMethod(Method method) {
        //获取serviceMethodCache的缓存
        ServiceMethod<?> result = serviceMethodCache.get(method);
        if (result != null) return result;

        synchronized (serviceMethodCache) {
            //根据method获取网络请求实例
            result = serviceMethodCache.get(method);
            //如果缓存是空的
            if (result == null) {
                //则解析method
                result = ServiceMethod.parseAnnotations(this, method);
                //解析完成后,存入serviceMethodCache,
                serviceMethodCache.put(method, result);
            }
        }
        //返回网络请求实例
        return result;
    }

先根据method从缓存serviceMethodCache读取网络请求实例,如果不是空则直接返回其实例对象。
否则通过 ServiceMethod.parseAnnotations(this, method)进行解析,完事后将其result实例对象存入缓存serviceMethodCache当中

abstract class ServiceMethod<T> {
  static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) {
  //主要的在这里,parseAnnotations
    RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method);

    Type returnType = method.getGenericReturnType();
    if (Utils.hasUnresolvableType(returnType)) {
      throw methodError(method,
          "Method return type must not include a type variable or wildcard: %s", returnType);
    }
    if (returnType == void.class) {
      throw methodError(method, "Service methods cannot return void.");
    }

    return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);
  }

  abstract T invoke(Object[] args);
}

由上可以看出,通过RequestFactory.parseAnnotations(retrofit, method);该方法进行retrofit,method进行解析生成RequestFactory 对象,将其对象传入HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);当中进行解析生成ServiceMethod网络请求对象。

static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations(
            Retrofit retrofit, Method method, RequestFactory requestFactory) {
        //创建适配器,RxJava
        CallAdapter<ResponseT, ReturnT> callAdapter = createCallAdapter(retrofit, method);
        //获取callAdapter响应类型
        Type responseType = callAdapter.responseType();
        if (responseType == Response.class || responseType == okhttp3.Response.class) {
            throw methodError(method, "'"
                    + Utils.getRawType(responseType).getName()
                    + "' is not a valid response body type. Did you mean ResponseBody?");
        }
        if (requestFactory.httpMethod.equals("HEAD") && !Void.class.equals(responseType)) {
            throw methodError(method, "HEAD method must use Void as response type.");
        }
        //创建数据类型转换器,比如Gson,
        Converter<ResponseBody, ResponseT> responseConverter =
                createResponseConverter(retrofit, method, responseType);
        //将其OkHttpClient对象 赋值给  callFactory
        okhttp3.Call.Factory callFactory = retrofit.callFactory;
        //将其解析完成的参数对象,传入HttpServiceMethod构造方法当中进行实例,并返回
        return new HttpServiceMethod<>(requestFactory, callFactory, callAdapter, responseConverter);
    }

通过retrofit,method,两个实例参数,通过createCallAdapter(retrofit, method);解析,并返回callAdapter实例对象。当适配器转换解析完成之后,通过createResponseConverter(retrofit, method, responseType);解析,返回responseConverter实例对象。最后返回new HttpServiceMethod<>(requestFactory, callFactory, callAdapter, responseConverter);实例对象。

    private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter(
            Retrofit retrofit, Method method) {
        //获取方法返回的数据类型模型
        Type returnType = method.getGenericReturnType();
        //获取方法注解
        Annotation[] annotations = method.getAnnotations();
        try {
            //noinspection unchecked
            //通过retrofit对象callAdapter方法进行解析,完事后返回适配器实例对象
            return (CallAdapter<ResponseT, ReturnT>) retrofit.callAdapter(returnType, annotations);
        } catch (RuntimeException e) { // Wide exception range because factories are user code.
            throw methodError(method, e, "Unable to create call adapter for %s", returnType);
        }
    }
    
 public CallAdapter<?, ?> callAdapter(Type returnType, Annotation[] annotations) {
    return nextCallAdapter(null, returnType, annotations);
  }

可以看到,把method.getGenericReturnType()数据类型模型与method.getAnnotations();注解数组,传入retrofit.callAdapter(returnType, annotations)方法进行解析,返回方法的适配器类型。

public CallAdapter<?, ?> nextCallAdapter(@Nullable CallAdapter.Factory skipPast, Type returnType,
                                             Annotation[] annotations) {
   	.....
   	
        //获取callAdapterFactories列表的索引,
        int start = callAdapterFactories.indexOf(skipPast) + 1;
        //针对列表进行循环查找,实例 CallAdapter适配器,返回该实例对象
        for (int i = start, count = callAdapterFactories.size(); i < count; i++) {
            CallAdapter<?, ?> adapter = callAdapterFactories.get(i).get(returnType, annotations, this);
            if (adapter != null) {
                return adapter;
            }
        }
	 
	 省略以下代码
     .....
        throw new IllegalArgumentException(builder.toString());
    }

针对callAdapterFactories 获取列表的位置,通过for循环查找callAdapterFactories.get(i).get(returnType, annotations, this);对象,返回其生成的对象

  private static <ResponseT> Converter<ResponseBody, ResponseT> createResponseConverter(
      Retrofit retrofit, Method method, Type responseType) {
    Annotation[] annotations = method.getAnnotations();
    try {
      return retrofit.responseBodyConverter(responseType, annotations);
    } catch (RuntimeException e) { // Wide exception range because factories are user code.
      throw methodError(method, e, "Unable to create converter for %s", responseType);
    }
  }
  
  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) {
        省略部分代码
        ...
        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;
            }
        }

        省略部分代码
        ...
        throw new IllegalArgumentException(builder.toString());
    }

针对converterFactories 获取列表的位置,通过for循环查找converterFactories.get(i).get(returnType, annotations, this);对象,返回其生成的对象

  private HttpServiceMethod(RequestFactory requestFactory, okhttp3.Call.Factory callFactory,
      CallAdapter<ResponseT, ReturnT> callAdapter,
      Converter<ResponseBody, ResponseT> responseConverter) {
    this.requestFactory = requestFactory;
    this.callFactory = callFactory;
    this.callAdapter = callAdapter;
    this.responseConverter = responseConverter;
  }

这是最后一步,实例new HttpServiceMethod<>(requestFactory, callFactory, callAdapter, responseConverter);对象。其实就是网络请求实例对象ServiceMethod<T>

//调用传入网络方法请求参数
 @Override ReturnT invoke(Object[] args) {
    return callAdapter.adapt(
        new OkHttpCall<>(requestFactory, args, callFactory, responseConverter));
  }

把以上所有的参数,通过联网OkHttpCall进行网络请求,并将后台返回的数据,进行解析回调处理。

1.网络请求接口类,

interface ApiService {

    @GET("/article/list/{page}/json")
    fun getListArticleData(@Path("page") page: Int): Call<BaseData<ArticleData>>

    @GET("/project/list/{page}/json")
    fun getListData(@Path("page") page: Int, @Query("cid") cid: Int): Call<BaseData<ArticleData>>
}

2.通用的接口地址类
注:使用的是鸿洋大神提供的接口地址,具体可以自行测试

object ApiUrl {

    val BASE_URL = "http://www.wanandroid.com"

    var ARTICLE_LIST = "/article/list/{page}/json"

}

3.使用方法

    //配置
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
  1.实例 Retrofit对象
  val retrofit = Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .baseUrl(ApiUrl.BASE_URL)
            .build()
  2.通过代理方法创建api接口类
  val apiService = retrofit.create(ApiService::class.java)
  3.调用接口内部的方法
  val listArticleData = apiService.getListArticleData(1)
  4.通过接口方法返回的实例进行回调,操作后台返回的数据
  listArticleData.enqueue(object : Callback<BaseData<ArticleData>> {
  //联网失败或数据解析失败
      override fun onFailure(call: Call<BaseData<ArticleData>>, t: Throwable) {
                println("t = $t")
            }
            //联网成功,后台返回数据
     override fun onResponse(call: Call<BaseData<ArticleData>>, response: Response<BaseData<ArticleData>>) {
                val mutableList: MutableList<ArticleData> = response.body()!!.data!!.datas as MutableList<ArticleData>
            }
        })

先告一段落。

总结,进过两天的源码研究。当你用心去看,总会有收获。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怀君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值