Retrofit2源碼解析

Retrofit2是針對(duì)于Android/Java的、基于okHttp的、一種輕量級(jí)且安全的、并使用注解方式的網(wǎng)絡(luò)請(qǐng)求框架。簡(jiǎn)單的說它是一個(gè)基于OkHttp的RESTFUL Api請(qǐng)求工具,它通過接口注解的方式,把注解信息封裝成一個(gè)Http請(qǐng)求,然后用OkHttp去發(fā)送這個(gè)請(qǐng)求。
以下是Retrofit的基本使用方法(代碼來(lái)自網(wǎng)絡(luò)):

public interface ZhuanLanApi {
    @GET("/api/columns/{user} ")
    Call<ZhuanLanAuthor> getAuthor(@Path("user") String user)
}
public static final String API_URL = "https://zhuanlan.zhihu.com";

Create a very simple REST adapter which points the Zhuanlan API.
Retrofit retrofit = new Retrofit.Builder()
    .baseUrl(API_URL)
    .addConverterFactory(GsonConverterFactory.create())
    .build();

ZhuanLanApi api = retrofit.create(ZhuanLanApi.class);
Call<ZhuanLanAuthor> call = api.getAuthor("qinchao");

// 請(qǐng)求數(shù)據(jù),并且處理response
call.enqueue(new Callback<ZhuanLanAuthor>() {
    @Override
    public void onResponse(Response<ZhuanLanAuthor> author) {
        System.out.println("name: " + author.getName());
    }
    @Override
    public void onFailure(Throwable t) {
    }
});

一、Retrofit的構(gòu)建

Retrofit通過Builder模式創(chuàng)建,我們先看它的成員變量:

    private Platform platform;//平臺(tái):安卓、java等
    private okhttp3.Call.Factory callFactory; //okhttp的Call工廠類,自定義newCall將Request轉(zhuǎn)為Call
    private HttpUrl baseUrl;//okhttp中的類,保存解析過的url
    private List<Converter.Factory> converterFactories = new ArrayList<>();//類型轉(zhuǎn)換工廠列表。
    private List<CallAdapter.Factory> adapterFactories = new ArrayList<>();//CallAdapter工廠列表。
    private Executor callbackExecutor;//回調(diào)線程池

Platform會(huì)根據(jù)不同平臺(tái)創(chuàng)建不同的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) {
    }
    return new Platform();
  }

  static class Android extends Platform {
    @Override public Executor defaultCallbackExecutor() {
      return new MainThreadExecutor();
    }

    @Override CallAdapter.Factory defaultCallAdapterFactory(@Nullable Executor callbackExecutor) {
      if (callbackExecutor == null) throw new AssertionError();
      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);
      }
    }
  }

MainThreadExecutor是主線程回調(diào)線程池,ExecutorCallAdapterFactory是默認(rèn)的類型轉(zhuǎn)換工廠。
build方法中,初始化了網(wǎng)絡(luò)請(qǐng)求Call、回調(diào)線程池callbackExecutor,以及Call適配器CallAdapter和類型轉(zhuǎn)換器converterFactories:

    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> adapterFactories = new ArrayList<>(this.adapterFactories);
      adapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));

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

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

二、ServiceMethod類:把接口注解數(shù)據(jù)轉(zhuǎn)出請(qǐng)求數(shù)據(jù)

當(dāng)我們要發(fā)起一個(gè)網(wǎng)絡(luò)請(qǐng)求時(shí),我們會(huì)先定義一個(gè)請(qǐng)求接口,通過create方法建構(gòu)一個(gè)Call,那么接口里設(shè)置的參數(shù)、信息是如果傳遞到網(wǎng)絡(luò)請(qǐng)求的呢?或者說,為什么一個(gè)接口就可以決定網(wǎng)絡(luò)請(qǐng)求的方式、網(wǎng)絡(luò)請(qǐng)求的請(qǐng)求數(shù)據(jù)呢?看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, @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);
            }
            ServiceMethod<Object, Object> serviceMethod =
                (ServiceMethod<Object, Object>) loadServiceMethod(method);
            OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
            return serviceMethod.callAdapter.adapt(okHttpCall);
          }
        });
  }

在create方法里面,讀取接口,并根據(jù)接口的名稱、注解、參數(shù),調(diào)用Java的動(dòng)態(tài)代理類生成請(qǐng)求。ServiceMethod類根據(jù)方法非返回值類型構(gòu)建請(qǐng)求適配器CallAdapter<T, R>和網(wǎng)絡(luò)結(jié)果轉(zhuǎn)換器Converter<ResponseBody, T>,根據(jù)方法注解和方法參數(shù)注解生成網(wǎng)絡(luò)請(qǐng)求。
首先會(huì)通過loadServiceMethod方法,調(diào)用serviceMethodCache是否已經(jīng)解析過這個(gè)方法,緩存擊中則返回緩存中的ServiceMethod。沒有擊中,則調(diào)用ServiceMethod的build方法構(gòu)建新的ServiceMethod。

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

    synchronized (serviceMethodCache) {
      result = serviceMethodCache.get(method);
      if (result == null) {
        result = new ServiceMethod.Builder<>(this, method).build();
        serviceMethodCache.put(method, result);
      }
    }
    return result;
  }

ServiceMethod的構(gòu)建由build生成,ServiceMethod的build方法拆分下面幾段來(lái)解析:

1、創(chuàng)建請(qǐng)求適配器CallAdapter<T, R> CallAdapter:

callAdapter = createCallAdapter();
    private CallAdapter<T, R> createCallAdapter() {
      Type returnType = method.getGenericReturnType();
      if (Utils.hasUnresolvableType(returnType)) {
        throw methodError(
            "Method return type must not include a type variable or wildcard: %s", returnType);
      }
      if (returnType == void.class) {
        throw methodError("Service methods cannot return void.");
      }
      Annotation[] annotations = method.getAnnotations();
      try {
        //noinspection unchecked
        return (CallAdapter<T, R>) retrofit.callAdapter(returnType, annotations);
      } catch (RuntimeException e) { // Wide exception range because factories are user code.
        throw methodError(e, "Unable to create call adapter for %s", returnType);
      }
    }

實(shí)際上這個(gè)Call適配器就是在Retrofit類中的adapterFactories,默認(rèn)情況下是在Retrofit的build方法里面初始化的ExecutorCallAdapterFactory,在retrofit.callAdapter中會(huì)調(diào)用它的get方法:

  @Override
  public CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    if (getRawType(returnType) != Call.class) {
      return null;
    }
    final Type responseType = Utils.getCallResponseType(returnType);
    return new CallAdapter<Object, Call<?>>() {
      @Override public Type responseType() {
        return responseType;
      }

      @Override public Call<Object> adapt(Call<Object> call) {
        return new ExecutorCallbackCall<>(callbackExecutor, call);
      }
    };
  }

所以,生成的CallAdapter中集成了回調(diào)線程池ExecutorCallbackCall,負(fù)責(zé)調(diào)用網(wǎng)絡(luò)請(qǐng)求enqueue以及分發(fā)成功與失敗的結(jié)果。

2、創(chuàng)建網(wǎng)絡(luò)結(jié)果轉(zhuǎn)換器Converter<ResponseBody, T> responseConverter:

responseConverter = createResponseConverter();
    private Converter<ResponseBody, T> createResponseConverter() {
      Annotation[] annotations = method.getAnnotations();
      try {
        return retrofit.responseBodyConverter(responseType, annotations);
      } catch (RuntimeException e) { // Wide exception range because factories are user code.
        throw methodError(e, "Unable to create converter for %s", responseType);
      }
    }

調(diào)用了Retrofit的responseBodyConverter方法,所以實(shí)際上是調(diào)用了BuiltInConverters的responseBodyConverter方法生成的:

  @Override
  public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
      Retrofit retrofit) {
    if (type == ResponseBody.class) {
      return Utils.isAnnotationPresent(annotations, Streaming.class)
          ? StreamingResponseBodyConverter.INSTANCE
          : BufferingResponseBodyConverter.INSTANCE;
    }
    if (type == Void.class) {
      return VoidResponseBodyConverter.INSTANCE;
    }
    return null;
  }

根據(jù)不同類型創(chuàng)建不同的轉(zhuǎn)換器Converter。BufferingResponseBodyConverter用于處理普通的網(wǎng)絡(luò)返回結(jié)果,StreamingResponseBodyConverter用于處理大文件下載的結(jié)果。

3、解析方法注解

      for (Annotation annotation : methodAnnotations) {
        parseMethodAnnotation(annotation);
      }

parseMethodAnnotation中會(huì)根據(jù)注解的類型(如GET、POST等),傳入?yún)?shù)調(diào)用注解解析器parseHttpMethodAndPath。
parseHttpMethodAndPath里面記錄了httpMethod(HTTP請(qǐng)求方式)、hasBody(是否有body)、relativeUrl(基地址之后的請(qǐng)求地址)、relativeUrlParamNames(方法注解中字段如@GET("http://www.ddd.com/{user}"))))

4、解析參數(shù)注解

      int parameterCount = parameterAnnotationsArray.length;
      parameterHandlers = new ParameterHandler<?>[parameterCount];
      for (int p = 0; p < parameterCount; p++) {
        Type parameterType = parameterTypes[p];
        if (Utils.hasUnresolvableType(parameterType)) {
          throw parameterError(p, "Parameter type must not include a type variable or wildcard: %s",
              parameterType);
        }

        Annotation[] parameterAnnotations = parameterAnnotationsArray[p];
        if (parameterAnnotations == null) {
          throw parameterError(p, "No Retrofit annotation found.");
        }

        parameterHandlers[p] = parseParameter(p, parameterType, parameterAnnotations);
      }

返回生成的ParameterHandler,生成方法在parseParameterAnnotation里面:

    private ParameterHandler<?> parseParameterAnnotation(
        int p, Type type, Annotation[] annotations, Annotation annotation) {
    ...
        
        else if (annotation instanceof Body) {
        if (isFormEncoded || isMultipart) {
          throw parameterError(p,
              "@Body parameters cannot be used with form or multi-part encoding.");
        }
        if (gotBody) {
          throw parameterError(p, "Multiple @Body method annotations found.");
        }

        Converter<?, RequestBody> converter;
        try {
          converter = retrofit.requestBodyConverter(type, annotations, methodAnnotations);
        } catch (RuntimeException e) {
          // Wide exception range because factories are user code.
          throw parameterError(e, p, "Unable to create @Body converter for %s", type);
        }
        gotBody = true;
        return new ParameterHandler.Body<>(converter);
      }
        
    ...    
    }

最終調(diào)用了Retrofit里的requestBodyConverter,也就是調(diào)用BuiltInConverters的requestBodyConverter(注意與上面的responseBodyConverter是不同的方法)生成(Converter<T, RequestBody>)

  @Override
  public Converter<?, RequestBody> requestBodyConverter(Type type,
      Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
    if (RequestBody.class.isAssignableFrom(Utils.getRawType(type))) {
      return RequestBodyConverter.INSTANCE;
    }
    return null;
  }

三、OkHttpCall:構(gòu)建網(wǎng)絡(luò)請(qǐng)求類

在create方法中,當(dāng)構(gòu)建完成ServiceMethod之后,就會(huì)出一個(gè)OkHttpCall,并設(shè)置給Retrofit的callAdapter,默認(rèn)下即ExecutorCallbackCall的delegate:

OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);

那么,我們通過create出來(lái)的call的enqueue方法發(fā)起網(wǎng)絡(luò)請(qǐng)求的時(shí)候,就是調(diào)用了OkHttpCall的enqueue方法,程序中,構(gòu)建了okhttp3所需的Call,并調(diào)用enqueue發(fā)起網(wǎng)絡(luò)請(qǐng)求:

  @Override public void enqueue(final Callback<T> callback) {
    checkNotNull(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) {
          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)
          throws IOException {
        Response<T> response;
        try {
          response = parseResponse(rawResponse);
        } catch (Throwable e) {
          callFailure(e);
          return;
        }
        callSuccess(response);
      }

      @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) {
          t.printStackTrace();
        }
      }

      private void callSuccess(Response<T> response) {
        try {
          callback.onResponse(OkHttpCall.this, response);
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
    });
  }

參考:
http://m.itdecent.cn/p/c1a3a881a144
http://blog.csdn.net/guiman/article/details/51480497

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 原文鏈接:http://bxbxbai.github.io/2015/12/13/retrofit2-analys...
    白瓦力閱讀 52,608評(píng)論 33 184
  • 本文的源碼分析基于Retrofit 2,和Retrofit 1.0的Api有較大的不同, 本文主要分為幾部分:1、...
    小帝Ele閱讀 320評(píng)論 0 1
  • 主目錄見:Android高級(jí)進(jìn)階知識(shí)(這是總目錄索引)?我們知道Retrofit2是基于OkHttp的一個(gè)Rest...
    ZJ_Rocky閱讀 1,592評(píng)論 0 6
  • 簡(jiǎn)介 剛接觸Retrofit的時(shí)候,就寫了一篇簡(jiǎn)單的使用介紹:Retrofit 2.0基本使用方法,算是對(duì)Retr...
    Whyn閱讀 3,111評(píng)論 4 24
  • 原文鏈接 http://www.qinglinyi.com/posts/retrofit/ 本文是關(guān)于Retrof...
    卓碼閱讀 1,004評(píng)論 0 1

友情鏈接更多精彩內(nèi)容