Retrofit分析

Retrofit分析(網絡封裝框架–>解耦)

需要關注的第一個點就是:create(Class service)

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 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);
            }
			//Retrofit的功能入口點
            return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
          }
        });
  }

loadServiceMethod(method).invoke(args != null ? args : emptyArgs);先看前半截的loadServiceMethod():

//返回的是ServiceMethod	
  ServiceMethod<?> loadServiceMethod(Method method) {
	//緩存  Map<Method, ServiceMethod<?>> serviceMethodCache = new ConcurrentHashMap<>();
	// 使用的是 ConcurrentHashMap,
    ServiceMethod<?> result = serviceMethodCache.get(method);
    if (result != null) return result;
	//對象鎖加持
    synchronized (serviceMethodCache) {
      result = serviceMethodCache.get(method);
      if (result == null) {
		//解析方法
        result = ServiceMethod.parseAnnotations(this, method);
		//放入map
        serviceMethodCache.put(method, result);
      }
    }
    return result;
  }

ServiceMethod.parseAnnotations

  // ServiceMethod.java
  static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) {
	###解析註解###
    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.");
    }
 //帶上解析出來的RequestFactory走進另一個類HttpServiceMethod
    return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);
  }

RequestFactory.parseAnnotations解析註解

 //builder設計模式
  static RequestFactory parseAnnotations(Retrofit retrofit, Method method) {
    return new Builder(retrofit, method).build();
  }

  Builder(Retrofit retrofit, Method method) {
  this.retrofit = retrofit;
  this.method = method;
  //解析註解
  this.methodAnnotations = method.getAnnotations();
  this.parameterTypes = method.getGenericParameterTypes();
  this.parameterAnnotationsArray = method.getParameterAnnotations();
}
**看build()方法
RequestFactory build() {
 //for循環註解,看來是要驗證註解的正確性,比如你寫了一個@Post(""),括號內的url是否正確
  for (Annotation annotation : methodAnnotations) {
###AAA#### 解析各種請求,以及url.詳情,請見下方AAA處
    parseMethodAnnotation(annotation);
  }
 ...
  return new RequestFactory(this);
}

AAA

//各種請求的驗證
private void parseMethodAnnotation(Annotation annotation) {
  if (annotation instanceof DELETE) {
    parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(), false);
  } else if (annotation instanceof GET) {
    parseHttpMethodAndPath("GET", ((GET) annotation).value(), false);
  } else if (annotation instanceof HEAD) {
    parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(), false);
  } else if (annotation instanceof PATCH) {
    parseHttpMethodAndPath("PATCH", ((PATCH) annotation).value(), true);
  } else if (annotation instanceof POST) {
    parseHttpMethodAndPath("POST", ((POST) annotation).value(), true);
  } else if (annotation instanceof PUT) {
    parseHttpMethodAndPath("PUT", ((PUT) annotation).value(), true);
  } else if (annotation instanceof OPTIONS) {
    parseHttpMethodAndPath("OPTIONS", ((OPTIONS) annotation).value(), false);
  } else if (annotation instanceof HTTP) {
    HTTP http = (HTTP) annotation;
    parseHttpMethodAndPath(http.method(), http.path(), http.hasBody());
  } else if (annotation instanceof retrofit2.http.Headers) {
    String[] headersToParse = ((retrofit2.http.Headers) annotation).value();
    if (headersToParse.length == 0) {
      throw methodError(method, "@Headers annotation is empty.");
    }
    headers = parseHeaders(headersToParse);
  } else if (annotation instanceof Multipart) {
    if (isFormEncoded) {
      throw methodError(method, "Only one encoding annotation is allowed.");
    }
    isMultipart = true;
  } else if (annotation instanceof FormUrlEncoded) {
    if (isMultipart) {
      throw methodError(method, "Only one encoding annotation is allowed.");
    }
    isFormEncoded = true;
  }
} 

private void parseHttpMethodAndPath(String httpMethod, String value, boolean hasBody) {
//一個請求就只能有一種方法 如@POST,@GET
  if (this.httpMethod != null) {
    throw methodError(method, "Only one HTTP method is allowed. Found: %s and %s.",
        this.httpMethod, httpMethod);
  }
  this.httpMethod = httpMethod;
  this.hasBody = hasBody;

  if (value.isEmpty()) {
    return;
  }

  // Get the relative URL path and existing query string, if present.
  int question = value.indexOf('?');
  if (question != -1 && question < value.length() - 1) {
    // Ensure the query string does not have any named parameters.
    String queryParams = value.substring(question + 1);
    Matcher queryParamMatcher = PARAM_URL_REGEX.matcher(queryParams);
    if (queryParamMatcher.find()) {
      throw methodError(method, "URL query string \"%s\" must not have replace block. "
          + "For dynamic query parameters use @Query.", queryParams);
    }
  }
 //相對url地址
  this.relativeUrl = value;
 //返回一個路徑,表示需要替換的參數
  this.relativeUrlParamNames = parsePathParameters(value);
}

回到ServiceMethod中:HttpServiceMethod.parseAnnotations (HttpServiceMethod繼承自ServiceMethod)

static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations(
  Retrofit retrofit, Method method, RequestFactory requestFactory) {
//是否支持kotlin的協程.默認時false
boolean isKotlinSuspendFunction = requestFactory.isKotlinSuspendFunction;
boolean continuationWantsResponse = false;
boolean continuationBodyNullable = false;

Annotation[] annotations = method.getAnnotations();
Type adapterType;
if (isKotlinSuspendFunction) {
	...
} else {
  adapterType = method.getGenericReturnType();
}
//這個CallAdapter就大有來頭了,需要深入分析一下
###BBB###
CallAdapter<ResponseT, ReturnT> callAdapter =
    createCallAdapter(retrofit, method, adapterType, annotations);
Type responseType = callAdapter.responseType();
//檢查返回的類型是否符合要求
if (responseType == okhttp3.Response.class) {
  throw methodError(method, "'"
      + getRawType(responseType).getName()
      + "' is not a valid response body type. Did you mean ResponseBody?");
}
if (responseType == Response.class) {
  throw methodError(method, "Response must include generic type (e.g., Response<String>)");
}
// TODO support Unit for Kotlin?
if (requestFactory.httpMethod.equals("HEAD") && !Void.class.equals(responseType)) {
  throw methodError(method, "HEAD method must use Void as response type.");
}

Converter<ResponseBody, ResponseT> responseConverter =
    createResponseConverter(retrofit, method, responseType);

okhttp3.Call.Factory callFactory = retrofit.callFactory;
if (!isKotlinSuspendFunction) {
	//默認情況下都是走這 ,CallAdapted繼承自HttpServiceMethod
	//###CCC###
  return new CallAdapted<>(requestFactory, callFactory, responseConverter, callAdapter);
} else if (continuationWantsResponse) {
  //noinspection unchecked Kotlin compiler guarantees ReturnT to be Object.
  return (HttpServiceMethod<ResponseT, ReturnT>) new SuspendForResponse<>(requestFactory,
      callFactory, responseConverter, (CallAdapter<ResponseT, Call<ResponseT>>) callAdapter);
} else {
  //noinspection unchecked Kotlin compiler guarantees ReturnT to be Object.
  return (HttpServiceMethod<ResponseT, ReturnT>) new SuspendForBody<>(requestFactory,
      callFactory, responseConverter, (CallAdapter<ResponseT, Call<ResponseT>>) callAdapter,
      continuationBodyNullable);
}}

BBB$createCallAdapter

  private static <ResponseT, ReturnT> CallAdapter<ResponseT, ReturnT> createCallAdapter(
      Retrofit retrofit, Method method, Type returnType, Annotation[] annotations) {
	  //調用retrofit的callAdapter
      return (CallAdapter<ResponseT, ReturnT>) retrofit.callAdapter(returnType, annotations);}
##callAdapter --> nextCallAdapter
public CallAdapter<?, ?> nextCallAdapter(@Nullable CallAdapter.Factory skipPast, Type returnType,
  Annotation[] annotations) {
//可以看出來尋找這個callAdapter要從callAdapterFactories這個集合中去尋找
//那麼這個集合是在哪裏初始化的了?
//細心的你應該已經發現,就是在Retrofit的內部類Build中完成了初始化
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;
  }
}

Retrofit$Build

Builder(Platform platform) {
  this.platform = platform;
}
public Builder() {
//Platform的get方法
  this(Platform.get());
}

class Platform {
 //PlatForm是一個單例設計模式
  private static final Platform PLATFORM = findPlatform();

  static Platform get() {
    return PLATFORM;
  }

  private static Platform findPlatform() {
    try {
      Class.forName("android.os.Build");
	//SDK的版本,當然不會爲0
      if (Build.VERSION.SDK_INT != 0) {
	//看到這裏new了一個Android類,其實這個Android類又繼承自Platform
        return new Android();
      }
    } catch (ClassNotFoundException ignored) {
    }
    return new Platform(true);
  }

  static final class Android extends Platform {
    Android() {
      super(Build.VERSION.SDK_INT >= 24);
    }

    @Override public Executor defaultCallbackExecutor() {
      return new MainThreadExecutor();
    }
   // 一個Executor
    static class MainThreadExecutor implements Executor {
	// 可以看到這是一個Handler拿了主線程的Looper
      private final Handler handler = new Handler(Looper.getMainLooper());
      @Override public void execute(Runnable r) {
		//也就是調用這個execute,一會兒傳進來的runable,回去執行的時候就在主線程了。
		//那麼什麼時候需要切換到主線程了,當然是使用okhttpClient的enqueue
        handler.post(r);}}}

通過上面的Platform分析,我們知道,一會兒調用defaultCallbackExecutor,會拿到一個Executor,執行executor(runbale)用來完成切換到主線程

public Retrofit build() {
  if (baseUrl == null) {
    throw new IllegalStateException("Base URL required.");
  }

  okhttp3.Call.Factory callFactory = this.callFactory;
  if (callFactory == null) {
	//如果沒有傳入OkhttpClient,那麼直接創建一個OkhttpClient
    callFactory = new OkHttpClient();
  }

  Executor callbackExecutor = this.callbackExecutor;
  if (callbackExecutor == null) {
	//如上所分析,這拿到這個executor,就是切換到主線程的executor
    callbackExecutor = platform.defaultCallbackExecutor();
  }
  // Make a defensive copy of the adapters and add the default Call adapter.
  List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
  //這來了一次裝飾,把這個executor封裝到了CallAdapter.Factory
  callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor));

platform.defaultCallAdapterFactories(callbackExecutor)

  List<? extends CallAdapter.Factory> defaultCallAdapterFactories(
      @Nullable Executor callbackExecutor) {
	//創建了一個 DefaultCallAdapterFactory
    DefaultCallAdapterFactory executorFactory = new DefaultCallAdapterFactory(callbackExecutor);
    return hasJava8Types
        ? asList(CompletableFutureCallAdapterFactory.INSTANCE, executorFactory)
        : singletonList(executorFactory);}

DefaultCallAdapterFactory

final class DefaultCallAdapterFactory extends CallAdapter.Factory {
  private final @Nullable Executor callbackExecutor;

  DefaultCallAdapterFactory(@Nullable Executor callbackExecutor) {
    this.callbackExecutor = callbackExecutor;
  }

  @Override public @Nullable CallAdapter<?, ?> get(
      Type returnType, Annotation[] annotations, Retrofit retrofit) {
	//Retrofit中創建CallAdapter的時候,調用get方法,執行到此處
	//可以看到new 了一個CallAdapter
    return new CallAdapter<Object, Call<?>>() {
      @Override public Type responseType() {
        return responseType;
      }

//下面這個adapt肯定要被回調,那什麼時候調用的了??
//回到Retrofit中create方法,的動態代理中 loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
//  @Override final @Nullable ReturnT invoke(Object[] args) {
// 創建了一個OkhttpCall
// Call<ResponseT> call = new OkHttpCall<>(requestFactory, args, callFactory, responseConverter);
// return adapt(call, args);}   此處的這個adapt爲抽象方法
//通過上面的分析,我們知道loadServiceMethod返回的是:CallAdapted實例
//CallAdapted的adapt()方法,又調用了 callAdapter.adapt(call),callAdapter正是我們當前正在分析的這個DefaultCallAdapterFactory中new 出來的callAdapter
所以動態代理中的invokde來到了下面這行代碼:

      @Override public Call<Object> adapt(Call<Object> call) { //所以傳進來的這個call就是OkhttpCall
        return executor == null
            ? call
            : new ExecutorCallbackCall<>(executor, call);
      }
    };
  }

  static final class ExecutorCallbackCall<T> implements Call<T> {
    final Executor callbackExecutor;
    final Call<T> delegate;

    ExecutorCallbackCall(Executor callbackExecutor, Call<T> delegate) {
      this.callbackExecutor = callbackExecutor;
      this.delegate = delegate; //這個delegate爲OkhttpCall
    }
	// 我們是不是使用Retrofit的時候,調用接口方之後就會 enqueue(new CallBack<T>{...}),
	//是的,所以我們調用的哪個enqueue的入口點,正是此處。
    @Override public void enqueue(final Callback<T> callback) {
      Objects.requireNonNull(callback, "callback == null");
	 // 去到OkhttpCall的enqueue方法,
      delegate.enqueue(new Callback<T>() {
        @Override public void onResponse(Call<T> call, final Response<T> response) {
			//可見CallbackExecutor執行execute(),讓這次請求回到了主線程中
          callbackExecutor.execute(() -> {
            if (delegate.isCanceled()) {
              // Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.
			 //終於回調到了用戶請求處
              callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
            } else {
              callback.onResponse(ExecutorCallbackCall.this, response);
            }
          });
        }

        @Override public void onFailure(Call<T> call, final Throwable t) {
          callbackExecutor.execute(() -> callback.onFailure(ExecutorCallbackCall.this, t));
        }
      });
    }

OkHttpCall的enqueue方法

@Override 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 {
	//這個叫做rawCall,可見不是一般的Call,沒錯,正是OKhttp的Call,而不是retrofit的Call
	// okhttp3.Call call = callFactory.newCall(requestFactory.create(args));
	//上面也就是 okhttp3.Call call = OkhttpClient.newCall( okhttp3.Request)
      call = rawCall = createRawCall();
    } catch (Throwable t) {
      throwIfFatal(t);
      failure = creationFailure = t;
    }
  }
}

if (failure != null) {
  callback.onFailure(this, failure);
  return;
}

if (canceled) {
  call.cancel();
}
//因此這個Call也就是OKhttpClient的call了,終於可以請求網絡
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 {
	//回調回去,準備讓executor來一次handler消息切換
      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(); // TODO this is not great
    }
  }
});}

至此,一次請求的大概過程就是這樣。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章