public interface GitHubService { @GET("users/{user}/repos") Call<List<Repo>> listRepos(@Path("user") String user);}複製程式碼
2、建立Retrofit並生成API的實現(注意:方法上面的註解表示請求的介面部分,返回型別是請求的返回值型別,方法的引數即是請求的引數)
// 1.Retrofit構建過程Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/").build();// 2.建立網路請求介面類例項過程GitHubService service = retrofit.create(GitHubService.class);複製程式碼
3、呼叫API方法,生成Call,執行請求// 3.生成並執行請求過程Call<List<Repo>> repos = service.listRepos("octocat");repos.execute() or repos.enqueue()複製程式碼
Retrofit的基本使用流程很簡潔,但是簡潔並不代表簡單,Retrofit為了實現這種簡潔的使用流程,內部使用了優秀的架構設計和大量的設計模式,在我分析過Retrofit最新版的原始碼和大量優秀的Retrofit原始碼分析文章後,我發現,要想真正理解Retrofit內部的核心原始碼流程和設計思想,首先,需要對這九大設計模式有一定的瞭解,如下:
1.Retrofit構建過程 建造者模式、工廠方法模式2.建立網路請求介面例項過程外觀模式、代理模式、單例模式、策略模式、裝飾模式(建造者模式)3.生成並執行請求過程介面卡模式(代理模式、裝飾模式)複製程式碼
其次,需要對OKHttp原始碼有一定的瞭解,如果不瞭解的可以看看這篇Android主流三方庫原始碼分析(一、深入理解OKHttp原始碼)。最後,讓我們按以上流程去深入Retrofit原始碼內部,領悟它帶給我們的設計之美。
二、Retrofit構建過程1、Retrofit核心物件解析首先Retrofit中有一個全域性變數非常關鍵,在V2.5之前的版本,使用的是LinkedHashMap(),它是一個網路請求配置物件,是由網路請求介面中方法註解進行解析後得到的。
public final class Retrofit { // 網路請求配置物件,儲存網路請求相關的配置,如網路請求的方法、資料轉換器、網路請求介面卡、網路請求工廠、基地址等 private final Map<Method, ServiceMethod<?>> serviceMethodCache = new ConcurrentHashMap<>();複製程式碼
Retrofit使用了建造者模式透過內部類Builder類建立一個Retrofit例項,如下:
public static final class Builder { // 平臺型別物件(Platform -> Android) private final Platform platform; // 網路請求工廠,預設使用OkHttpCall(工廠方法模式) private @Nullable okhttp3.Call.Factory callFactory; // 網路請求的url地址 private @Nullable HttpUrl baseUrl; // 資料轉換器工廠的集合 private final List<Converter.Factory> converterFactories = new ArrayList<>(); // 網路請求介面卡工廠的集合,預設是ExecutorCallAdapterFactory private final List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(); // 回撥方法執行器,在 Android 上預設是封裝了 handler 的 MainThreadExecutor, 預設作用是:切換執行緒(子執行緒 -> 主執行緒) private @Nullable Executor callbackExecutor; // 一個開關,為true則會快取建立的ServiceMethod private boolean validateEagerly;複製程式碼
2、Builder內部構造下面看看Builder內部構造做了什麼。
public static final class Builder { ... Builder(Platform platform) { this.platform = platform; } public Builder() { this(Platform.get()); } ...}class Platform { private static final Platform PLATFORM = findPlatform(); static Platform get() { return PLATFORM; } private static Platform findPlatform() { try { // 使用JVM載入類的方式判斷是否是Android平臺 Class.forName("android.os.Build"); if (Build.VERSION.SDK_INT != 0) { return new Android(); } } catch (ClassNotFoundException ignored) { } try { // 同時支援Java平臺 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(); } // 建立預設的網路請求介面卡工廠,如果是Android7.0或Java8上,則使 // 用了併發包中的CompletableFuture保證了回撥的同步 // 在Retrofit中提供了四種CallAdapterFactory(策略模式): // ExecutorCallAdapterFactory(預設)、GuavaCallAdapterFactory、 // va8CallAdapterFactory、RxJavaCallAdapterFactory @Override List<? extends CallAdapter.Factory> defaultCallAdapterFactories( @Nullable Executor callbackExecutor) { if (callbackExecutor == null) throw new AssertionError(); ExecutorCallAdapterFactory executorFactory = new ExecutorCallAdapterFactory(callbackExecutor); return Build.VERSION.SDK_INT >= 24 ? asList(CompletableFutureCallAdapterFactory.INSTANCE, executorFactory) : singletonList(executorFactory); } ... @Override List<? extends Converter.Factory> defaultConverterFactories() { return Build.VERSION.SDK_INT >= 24 ? singletonList(OptionalConverterFactory.INSTANCE) : Collections.<Converter.Factory>emptyList(); } ... static class MainThreadExecutor implements Executor { // 獲取Android 主執行緒的Handler private final Handler handler = new Handler(Looper.getMainLooper()); @Override public void execute(Runnable r) { // 在UI執行緒對網路請求返回資料處理 handler.post(r); } }}複製程式碼
可以看到,在Builder內部構造時設定了預設Platform、callAdapterFactories和callbackExecutor。
3、新增baseUrl很簡單,就是將String型別的url轉換為OkHttp的HttpUrl過程如下:
/** * Set the API base URL. * * @see #baseUrl(HttpUrl) */public Builder baseUrl(String baseUrl) { checkNotNull(baseUrl, "baseUrl == null"); return baseUrl(HttpUrl.get(baseUrl));}public Builder baseUrl(HttpUrl baseUrl) { checkNotNull(baseUrl, "baseUrl == null"); List<String> pathSegments = baseUrl.pathSegments(); if (!"".equals(pathSegments.get(pathSegments.size() - 1))) { throw new IllegalArgumentException("baseUrl must end in /: " + baseUrl); } this.baseUrl = baseUrl; return this;}複製程式碼
4、新增GsonConverterFactory首先,看到GsonConverterFactory.creat()的原始碼。
public final class GsonConverterFactory extends Converter.Factory { public static GsonConverterFactory create() { return create(new Gson()); } public static GsonConverterFactory create(Gson gson) { if (gson == null) throw new NullPointerException("gson == null"); return new GsonConverterFactory(gson); } private final Gson gson; // 建立了一個含有Gson物件例項的GsonConverterFactory private GsonConverterFactory(Gson gson) { this.gson = gson; }複製程式碼
然後,看看addConverterFactory()方法內部。
public Builder addConverterFactory(Converter.Factory factory) { converterFactories.add(checkNotNull(factory, "factory null")); return this;}複製程式碼
可知,這一步是將一個含有Gson物件例項的GsonConverterFactory放入到了資料轉換器工廠converterFactories裡。
5、build過程public Retrofit build() { if (baseUrl == null) { throw new IllegalStateException("Base URL required."); } okhttp3.Call.Factory callFactory = this.callFactory; if (callFactory == null) { // 預設使用okhttp callFactory = new OkHttpClient(); } Executor callbackExecutor = this.callbackExecutor; if (callbackExecutor == null) { // Android預設的callbackExecutor callbackExecutor = platform.defaultCallbackExecutor(); } // Make a defensive copy of the adapters and add the defaultCall adapter. List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories); // 新增預設介面卡工廠在集合尾部 callAdapterFactories.addAll(platform.defaultCallAdapterFactorisca llbackExecutor)); // 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 thatconsumeall types. converterFactories.add(new BuiltInConverters()); converterFactories.addAll(this.converterFactories); converterFactories.addAll(platform.defaultConverterFactories(); return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories), unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);}複製程式碼
可以看到,最終我們在Builder類中看到的6大核心物件都已經配置到Retrofit物件中了。
三、建立網路請求介面例項過程retrofit.create()使用了外觀模式和代理模式建立了網路請求的介面例項,我們分析下create方法。
public <T> T create(final Class<T> service) { Utils.validateServiceInterface(service); if (validateEagerly) { // 判斷是否需要提前快取ServiceMethod物件 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); } }); }private void eagerlyValidateMethods(Class<?> service) { Platform platform = Platform.get(); for (Method method : service.getDeclaredMethods()) { if (!platform.isDefaultMethod(method)) { loadServiceMethod(method); } }}複製程式碼
繼續看看loadServiceMethod的內部流程
ServiceMethod<?> loadServiceMethod(Method method) { ServiceMethod<?> result = serviceMethodCache.get(method); if (result != null) return result; synchronized (serviceMethodCache) { result = serviceMethodCache.get(method); if (result == null) { // 解析註解配置得到了ServiceMethod result = ServiceMethod.parseAnnotations(this, method); // 可以看到,最終加入到ConcurrentHashMap快取中 serviceMethodCache.put(method, result); } } return result;}abstract class ServiceMethod<T> { static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) { // 透過RequestFactory解析註解配置(工廠模式、內部使用了建造者模式) 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."); } // 最終是透過HttpServiceMethod構建的請求方法 return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory); } abstract T invoke(Object[] args);}複製程式碼
以下為請求構造核心流程根據RequestFactory#Builder構造方法和parseAnnotations方法的原始碼,可知的它的作用就是用來解析註解配置的。
Builder(Retrofit retrofit, Method method) { this.retrofit = retrofit; this.method = method; // 獲取網路請求介面方法裡的註釋 this.methodAnnotations = method.getAnnotations(); // 獲取網路請求介面方法裡的引數型別 this.parameterTypes = method.getGenericParameterTypes(); // 獲取網路請求介面方法裡的註解內容 this.parameterAnnotationsArray = method.getParameterAnnotations();}複製程式碼
接著看HttpServiceMethod.parseAnnotations()的內部流程。
static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations( Retrofit retrofit, Method method, RequestFactory requestFactory) { //1.根據網路請求介面方法的返回值和註解型別, // 從Retrofit物件中獲取對應的網路請求介面卡 CallAdapter<ResponseT, ReturnT> callAdapter = createCallAdapter(retrofit,method); // 得到響應型別 Type responseType = callAdapter.responseType(); ... //2.根據網路請求介面方法的返回值和註解型別從Retrofit物件中獲取對應的資料轉換器 Converter<ResponseBody, ResponseT>responseConverter = createResponseConverter(retrofit,method, responseType); okhttp3.Call.Factory callFactory = retrofit.callFactory; return newHttpServiceMethod<>(requestFactory, callFactory, callAdapter,responseConverter);}複製程式碼
1.createCallAdapter(retrofit, method)private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter( Retrofit retrofit, Method method) { // 獲取網路請求接口裡方法的返回值型別 Type returnType = method.getGenericReturnType(); // 獲取網路請求介面接口裡的註解 Annotation[] annotations = method.getAnnotations(); try { //noinspection unchecked 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);}public CallAdapter<?, ?> nextCallAdapter(@Nullable CallAdapter.Factory skipPast, Type returnType, Annotation[] annotations) { ... int start = callAdapterFactories.indexOf(skipPast) + 1; // 遍歷 CallAdapter.Factory 集合尋找合適的工廠 for (int i = start, count = callAdapterFactories.size(); i <count; i++) { CallAdapter<?, ?> adapter = callAdapterFactories.get(i).get(returnType, annotations, this); if (adapter != null) { return adapter; } }}複製程式碼
2.createResponseConverter(Retrofit retrofit, Method method, Type responseType) 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;// 遍歷 Converter.Factory 集合並尋找合適的工廠, 這裡是GsonResponseBodyConverterfor (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; }}複製程式碼
最終,執行HttpServiceMethod的invoke方法@Override ReturnT invoke(Object[] args) { return callAdapter.adapt( new OkHttpCall<>(requestFactory, args, callFactory, responseConverter));}複製程式碼
最終在adapt中建立了一個ExecutorCallbackCall物件,它是一個裝飾者,而在它內部真正去執行網路請求的還是OkHttpCall。
四、建立網路請求介面類例項並執行請求過程1、service.listRepos()1、Call<List<Repo>> repos = service.listRepos("octocat");複製程式碼
service物件是動態代理物件Proxy.newProxyInstance(),當呼叫getCall()時會被 它攔截,然後呼叫自身的InvocationHandler#invoke(),得到最終的Call物件。
2、同步執行流程 repos.execute()@Override public Response<T> execute() throws IOException { okhttp3.Call call; synchronized (this) { if (executed) throw new IllegalStateException("Already executed."); executed = true; if (creationFailure != null) { if (creationFailure instanceof IOException) { throw (IOException) creationFailure; } else if (creationFailure instanceof RuntimeException) { throw (RuntimeException) creationFailure; } else { throw (Error) creationFailure; } } call = rawCall; if (call == null) { try { // 建立一個OkHttp的Request物件請求 call = rawCall = createRawCall(); } catch (IOException | RuntimeException | Error e) { throwIfFatal(e); // Do not assign a fatal error to creationFailure. creationFailure = e; throw e; } } } if (canceled) { call.cancel(); } // 呼叫OkHttpCall的execute()傳送網路請求(同步), // 並解析網路請求返回的資料 return parseResponse(call.execute());}private okhttp3.Call createRawCall() throws IOException { // 建立 一個okhttp3.Request okhttp3.Call call = callFactory.newCall(requestFactory.create(args)); if (call == null) { throw new NullPointerException("Call.Factory returned null."); } return call;}Response<T> parseResponse(okhttp3.Response rawResponse) throws IOException { ResponseBody rawBody = rawResponse.body(); // Remove the body's source (the only stateful object) so we can pass the response along. rawResponse = rawResponse.newBuilder() .body(new NoContentResponseBody(rawBody.contentType(), rawBody.contentLength())) .build(); // 根據響應返回的狀態碼進行處理 int code = rawResponse.code(); if (code < 200 || code >= 300) { try { // Buffer the entire body to avoid future I/O. ResponseBody bufferedBody = Utils.buffer(rawBody); return Response.error(bufferedBody, rawResponse); } finally { rawBody.close(); } } if (code == 204 || code == 205) { rawBody.close(); return Response.success(null, rawResponse); } ExceptionCatchingResponseBody catchingBody = new ExceptionCatchingResponseBody(rawBody); try { // 將響應體轉為Java物件 T body = responseConverter.convert(catchingBody); return Response.success(body, rawResponse); } catch (RuntimeException e) { // If the underlying source threw an exception, propagate that rather than indicating it was // a runtime exception. catchingBody.throwIfCaught(); throw e; }}複製程式碼
3、非同步請求流程 reponse.enqueque@Override public void enqueue(final Callback<T> callback) { // 使用靜態代理 delegate進行非同步請求 delegate.enqueue(new Callback<T>() { @Override public void onResponse(Call<T> call, finalResponse<T>response) { // 執行緒切換,在主執行緒顯示結果 callbackExecutor.execute(new Runnable() { @Override public void run() { if (delegate.isCanceled()) { callback.onFailure(ExecutorCallbackCall.this, newIOException("Canceled")); } else { callback.onResponse(ExecutorCallbackCall.this,respons); } } }); } @Override public void onFailure(Call<T> call, final Throwable t) { callbackExecutor.execute(new Runnable() { @Override public void run() { callback.onFailure(ExecutorCallbackCall.this, t); } }); } });}複製程式碼
看看 delegate.enqueue 內部流程。
@Override public void enqueue(final Callback<T> callback) { 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 { // 建立OkHttp的Request物件,再封裝成OkHttp.call // 方法同傳送同步請求,此處上面已分析 call = rawCall = createRawCall(); } catch (Throwable t) { failure = creationFailure = t; } }@Override public void enqueue(final Callback<T> callback) { checkNotNull(callback, "callback == null"); okhttp3.Call call; Throwable failure; ... 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) { t.printStackTrace(); } } @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(); } } });}複製程式碼
如果你看到這裡的話,恭喜你,你已經對Retrofit已經有一個比較深入的瞭解了,但是,筆者還是要建議大家自己主動配合著Retrofit最新版的原始碼一步步去徹底地認識它,只有這樣,你才能看到它真實的內心,最後附上一張Stay的Retrofit原始碼流程圖,要注意的是,這是V2.5之前版本的流程,但是,在看完上面的原始碼分析後,我們知道,主體流程是沒有變化的。
五、總結從本質上來說,Retrofit雖然只是一個RESTful 的HTTP 網路請求框架的封裝庫。但是,它內部透過 大量的設計模式 封裝了 OkHttp,讓使用者感到它非常簡潔、易懂。它內部主要是用動態代理的方式,動態將網路請求介面的註解解析成HTTP請求,最後執行請求的過程。好了,至此,我們的Android主流三方庫原始碼分析的網路庫分析部分已經完畢。接下來,將為大家帶來最流行的圖片載入框架Glide的原始碼分析,敬請期待~
Android相關原始碼解讀目錄
Android相關原始碼解讀部分解析內容
開源框架解讀目錄及部分解析內容