綜述
Retrofit到一個(gè)實(shí)際請(qǐng)求/響應(yīng)的過程包括
- 創(chuàng)建ServiceMethod,調(diào)用
ServiceMethod.build() - 調(diào)用OkHttpCall(ServiceMethod,args)創(chuàng)建OkHttpCall對(duì)象,
- 調(diào)用 CallAdapter.adapt(okHttpCall)創(chuàng)建關(guān)聯(lián)執(zhí)行器Executor和Call的應(yīng)用層Call類,Android默認(rèn)為ExecutorCallbackCall
源碼研究
- 創(chuàng)建ServiceMethod
ServiceMethod.build()
public ServiceMethod build() {
//通過Retrofit.nextCallAdapter獲取callAdapter,有自定義就用自定義,沒有就用默認(rèn)ExecutorCallAdapterFactory
callAdapter = createCallAdapter();
responseType = callAdapter.responseType();
//返回類型驗(yàn)證
if (responseType == Response.class || responseType == okhttp3.Response.class) {
throw methodError("'"
+ Utils.getRawType(responseType).getName()
+ "' is not a valid response body type. Did you mean ResponseBody?");
}
//通過Retrofit.nextResponseBodyConverter處理轉(zhuǎn)換異常,獲得自定義轉(zhuǎn)換器
responseConverter = createResponseConverter();
//解析自定義的注解成URL組件
for (Annotation annotation : methodAnnotations) {
parseMethodAnnotation(annotation);
}
//錯(cuò)誤處理
...
//構(gòu)建參數(shù)替換的輔助數(shù)組parameterHandlers
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);
}
//錯(cuò)誤處理
...
return new ServiceMethod<>(this);
}
- 用戶調(diào)用enqueue方法發(fā)起異步請(qǐng)求
ExecutorCallbackCall.enqueue()
@Override public void enqueue(final Callback<T> callback) {
checkNotNull(callback, "callback == null");
//調(diào)用OkhttpCall的enqueue方法,Executor執(zhí)行響應(yīng)結(jié)果
delegate.enqueue(new Callback<T>() {
}
});
}
OkHttpCall. enqueue
@Override public void enqueue(final Callback<T> callback) {
checkNotNull(callback, "callback == null");
okhttp3.Call call;
Throwable failure;
synchronized (this) {
...
//這里創(chuàng)建請(qǐng)求報(bào)文獲得realCall
call = rawCall = createRawCall();
...
}
//realCall發(fā)起異步請(qǐng)求
call.enqueue(new okhttp3.Callback() {
...
//轉(zhuǎn)換響應(yīng)報(bào)文
response = parseResponse(rawResponse);
...
});
}
createRawCall
private okhttp3.Call createRawCall() throws IOException {
//創(chuàng)建request報(bào)文
Request request = serviceMethod.toRequest(args);
//獲得okhttp.realCall
okhttp3.Call call = serviceMethod.callFactory.newCall(request);
if (call == null) {
throw new NullPointerException("Call.Factory returned null.");
}
return call;
}
serviceMethod.toRequest(args)
Request toRequest(@Nullable Object... args) throws IOException {
RequestBuilder requestBuilder = new RequestBuilder(httpMethod, baseUrl, relativeUrl, headers,
contentType, hasBody, isFormEncoded, isMultipart);
...
@SuppressWarnings("unchecked") // It is an error to invoke a method with the wrong arg types.
ParameterHandler<Object>[] handlers = (ParameterHandler<Object>[]) parameterHandlers;
//根據(jù)先前的parameterHandlers進(jìn)行參數(shù)轉(zhuǎn)換
for (int p = 0; p < argumentCount; p++) {
handlers[p].apply(requestBuilder, args[p]);
}
//創(chuàng)建request報(bào)文
return requestBuilder.build();
}
parseResponse
Response<T> parseResponse(okhttp3.Response rawResponse) throws IOException {
ResponseBody rawBody = rawResponse.body();
//異常處理,200-299成功狀態(tài)碼,204和205沒有響應(yīng)body
...
//serviceMethod.toResponse用戶轉(zhuǎn)換器轉(zhuǎn)換
T body = serviceMethod.toResponse(catchingBody);
return Response.success(body, rawResponse);
}
總結(jié)
通過梳理大體了解的Retrofit的整體結(jié)構(gòu)和流程,當(dāng)然還有很多的細(xì)節(jié)沒有討論,需要時(shí)可以查看相應(yīng)的源碼。
Retrofit源碼本身并不復(fù)雜,但是Retrofit的架構(gòu)設(shè)計(jì),解耦所透露的架構(gòu)經(jīng)驗(yàn),思想,規(guī)范等是非常值得我們學(xué)習(xí)和參照設(shè)計(jì)的。