Retrofit2源碼解析

Retrofit2是針對于Android/Java的、基于okHttp的、一種輕量級且安全的、并使用注解方式的網絡請求框架。簡單的說它是一個基于OkHttp的RESTFUL Api請求工具,它通過接口注解的方式,把注解信息封裝成一個Http請求,然后用OkHttp去發送這個請求。
以下是Retrofit的基本使用方法(代碼來自網絡):

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");

// 請求數據,并且處理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的構建

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

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

Platform會根據不同平臺創建不同的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是主線程回調線程池,ExecutorCallAdapterFactory是默認的類型轉換工廠。
build方法中,初始化了網絡請求Call、回調線程池callbackExecutor,以及Call適配器CallAdapter和類型轉換器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類:把接口注解數據轉出請求數據

當我們要發起一個網絡請求時,我們會先定義一個請求接口,通過create方法建構一個Call,那么接口里設置的參數、信息是如果傳遞到網絡請求的呢?或者說,為什么一個接口就可以決定網絡請求的方式、網絡請求的請求數據呢?看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方法里面,讀取接口,并根據接口的名稱、注解、參數,調用Java的動態代理類生成請求。ServiceMethod類根據方法非返回值類型構建請求適配器CallAdapter<T, R>和網絡結果轉換器Converter<ResponseBody, T>,根據方法注解和方法參數注解生成網絡請求。
首先會通過loadServiceMethod方法,調用serviceMethodCache是否已經解析過這個方法,緩存擊中則返回緩存中的ServiceMethod。沒有擊中,則調用ServiceMethod的build方法構建新的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的構建由build生成,ServiceMethod的build方法拆分下面幾段來解析:

1、創建請求適配器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);
      }
    }

實際上這個Call適配器就是在Retrofit類中的adapterFactories,默認情況下是在Retrofit的build方法里面初始化的ExecutorCallAdapterFactory,在retrofit.callAdapter中會調用它的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中集成了回調線程池ExecutorCallbackCall,負責調用網絡請求enqueue以及分發成功與失敗的結果。

2、創建網絡結果轉換器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);
      }
    }

調用了Retrofit的responseBodyConverter方法,所以實際上是調用了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;
  }

根據不同類型創建不同的轉換器Converter。BufferingResponseBodyConverter用于處理普通的網絡返回結果,StreamingResponseBodyConverter用于處理大文件下載的結果。

3、解析方法注解

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

parseMethodAnnotation中會根據注解的類型(如GET、POST等),傳入參數調用注解解析器parseHttpMethodAndPath。
parseHttpMethodAndPath里面記錄了httpMethod(HTTP請求方式)、hasBody(是否有body)、relativeUrl(基地址之后的請求地址)、relativeUrlParamNames(方法注解中字段如@GET("http://www.ddd.com/{user}"))))

4、解析參數注解

      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);
      }
        
    ...    
    }

最終調用了Retrofit里的requestBodyConverter,也就是調用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:構建網絡請求類

在create方法中,當構建完成ServiceMethod之后,就會出一個OkHttpCall,并設置給Retrofit的callAdapter,默認下即ExecutorCallbackCall的delegate:

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

那么,我們通過create出來的call的enqueue方法發起網絡請求的時候,就是調用了OkHttpCall的enqueue方法,程序中,構建了okhttp3所需的Call,并調用enqueue發起網絡請求:

  @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://www.lxweimin.com/p/c1a3a881a144
http://blog.csdn.net/guiman/article/details/51480497

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容