/**
* 初始化Retrofit
*/
public static void init() {
okHttpClient = HttpsUtils.getOKHttpClient();
//設置Retrofit
Retrofit client = new Retrofit.Builder()
.baseUrl(HOST)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(okHttpClient)
.build();
//建立業務請求對象
api = client.create(RequestApi.class);
}
複製代碼
public Retrofit build() {
//校驗baseUrl
if (baseUrl == null) {
throw new IllegalStateException("Base URL required.");
}
//獲取回調工廠,默認使用okhttp
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {
callFactory = new OkHttpClient();
}
//添加線程池
Executor callbackExecutor = this.callbackExecutor;
if (callbackExecutor == null) {
callbackExecutor = platform.defaultCallbackExecutor();
}
// 調用相關的適配器
List<CallAdapter.Factory> adapterFactories = new ArrayList<>(this.adapterFactories);
adapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));
// 執行相關的轉換器
List<Converter.Factory> converterFactories = new ArrayList<>(this.converterFactories);
return new Retrofit(callFactory, baseUrl, converterFactories, adapterFactories,
callbackExecutor, validateEagerly);
}
複製代碼
buid方法主要是構建一個真正的retrofit對象,加載適配器,轉化器,callbackExecutor線程池,baseUrl等基本數據java
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() {
//獲取對應的支持的平臺 android java
private final Platform platform = Platform.get();
@Override public Object invoke(Object proxy, Method method, 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);
}
});
}
複製代碼
動態代理的核心方法,都會調用invoke,前面會加一些方法校驗,並且retrofit一樣支持android和java,因此也對平臺作了封裝,同時也加map緩存對應的代理對象android
ServiceMethod<?, ?> loadServiceMethod(Method method) {
ServiceMethod<?, ?> result = serviceMethodCache.get(method);
if (result != null) return result;
//加一個異步鎖
synchronized (serviceMethodCache) {
//從的map中去拿
result = serviceMethodCache.get(method);
if (result == null) {
result = new ServiceMethod.Builder<>(this, method).build();
serviceMethodCache.put(method, result);
}
}
return result;
}
複製代碼
加了一個同步鎖,防止多線程的數據混亂問題,map緩存增長執行速度,構建以後一樣加入map中去api
public ServiceMethod build() {
//構建響應Call的適配器,能夠使用者經過addCallAdapterFactory() 工廠類添加
callAdapter = createCallAdapter();
//.....響應適配器的返回類型校驗
// 建立響應的Converter變換器 可經過addConverterFactory 工廠類添加
responseConverter = createResponseConverter();
//一系列的請求註解校驗,具體看源碼
return new ServiceMethod<>(this);
}
複製代碼
構建響應的適配器和轉換器,這些均可以被使用者經過工廠類從外部傳進來緩存
ServiceMethod類:
private CallAdapter<T, R> createCallAdapter() {
//獲取接口指定的返回類型,Call或者是Observatble
Type returnType = method.getGenericReturnType();
//省略部分校驗
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);
}
}
//Retrofit的callAdapter方法
public CallAdapter<?, ?> callAdapter(Type returnType, Annotation[] annotations) {
return nextCallAdapter(null, returnType, annotations);
}
public CallAdapter<?, ?> nextCallAdapter(CallAdapter.Factory skipPast, Type returnType,
Annotation[] annotations) {
checkNotNull(returnType, "returnType == null");
checkNotNull(annotations, "annotations == null");
int start = adapterFactories.indexOf(skipPast) + 1;
for (int i = start, count = adapterFactories.size(); i < count; i++) {
CallAdapter<?, ?> adapter = adapterFactories.get(i).get(returnType, annotations, this);
if (adapter != null) {
return adapter;
}
}
//省略
}
複製代碼
createCallAdapter校驗返回值類型 以後會回調Retrofit的callAdapter方法以後執行適配器list中的適配器get方法bash
@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);
}
};
}
複製代碼
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;
}
@Override public void enqueue(final Callback<T> callback) {
if (callback == null) throw new NullPointerException("callback == null");
調用okhtt.Call方法裏面的enqueue
delegate.enqueue(new Callback<T>() {
@Override public void onResponse(Call<T> call, final Response<T> response) {
callbackExecutor.execute(new Runnable() {
@Override public void run() {
if (delegate.isCanceled()) {
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(new Runnable() {
@Override public void run() {
callback.onFailure(ExecutorCallbackCall.this, t);
}
});
}
});
}
//省略
}
複製代碼
進入了okhttp的請求中多線程
這裏感受比較巧妙,基本都是使用handler,在Platform類中定義android平臺時能夠看到以下一段代碼恰好和上面的ExecutorCallbackCall代理類delegate.enqueue(new Callback())呼應異步
static class Android extends Platform {
@Override public Executor defaultCallbackExecutor() {
return new MainThreadExecutor();
}
@Override CallAdapter.Factory defaultCallAdapterFactory(Executor callbackExecutor) {
return new ExecutorCallAdapterFactory(callbackExecutor);
}
static class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
//執行execute用handler的post方法到主線程
@Override public void execute(Runnable r) {
handler.post(r);
}
}
}
複製代碼
利用主線程的handler的post方法在執行execute的時候把數據返回主線程ide
最後加上流程圖 oop