前面已經(jīng)分析了OkHttp源碼的調(diào)用流程慷垮,通常會搭配Retrofit一起使用∽岫椋籠統(tǒng)地說料身,Retrofit只是一個適配器,它內(nèi)部封裝了OkHttp鹤啡,并不做實(shí)際的網(wǎng)絡(luò)請求工作惯驼,而主要是為客戶端調(diào)用網(wǎng)絡(luò)請求和返回?cái)?shù)據(jù)做適配,因此递瑰,我們說Retrofit只是一個適配器框架祟牲。同樣的,先看下它的使用方法抖部。
public interface RetrofitService {
@GET("users/{user}/repos")
Call<List<String>> listRepos(@Path("user") String user);
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl( "https://api.github.com/" )//網(wǎng)絡(luò)請求根地址说贝,必須有,且末尾帶/
.addConverterFactory( GsonConverterFactory.create() )//數(shù)據(jù)解析適配器
.addCallAdapterFactory( RxJava2CallAdapterFactory.create() )//請求適配器
.build();
//獲取接口對象
RetrofitService retrofitService = retrofit.create( RetrofitService.class );
//獲取OkHttpCall
Call repos = retrofitService.listRepos( "XXX" );
//同步請求
try {
Response execute = repos.execute();
} catch (IOException e) {
e.printStackTrace();
}
//異步請求
repos.enqueue( new Callback() {
@Override
public void onResponse(Call call, Response response) {
}
@Override
public void onFailure(Call call, Throwable t) {
}
} );
從示例看到慎颗,Retrofit只是做接口解析封裝乡恕,這里的Call是Retrofit實(shí)現(xiàn)的一個裝飾類,叫OkHttpCall俯萎,它持有OkHttp的Call 傲宜,即RealCall,由RealCall來做實(shí)際的網(wǎng)絡(luò)請求夫啊。
下面我們先分析Retrofit的構(gòu)建過程函卒。
public static final class Builder {
//平臺,默認(rèn)是Android
private final Platform platform;
private @Nullable okhttp3.Call.Factory callFactory;
//基地址
private @Nullable HttpUrl baseUrl;
//數(shù)據(jù)適配器列表
private final List<Converter.Factory> converterFactories = new ArrayList<>();
//請求適配器列表
private final List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>();
private @Nullable Executor callbackExecutor;
private boolean validateEagerly;
Builder(Platform platform) {
this.platform = platform;
}
public Builder() {
this(Platform.get());
}
......
}
Retrofit也是用構(gòu)建者模式進(jìn)行創(chuàng)建的撇眯,它的Builder構(gòu)造參數(shù)需傳入一個Platform對象报嵌,這意味著,Retrofit不僅可以使用在Android平臺熊榛,也可以使用在Java平臺中锚国。如果我們不傳入指定平臺,則會調(diào)用Platform.get()玄坦,它默認(rèn)返回了Android對象血筑。
class Platform {
private static final Platform PLATFORM = findPlatform();
//獲取默認(rèn)平臺
static Platform get() {
return PLATFORM;
}
//獲取平臺
private static Platform findPlatform() {
try {
Class.forName("android.os.Build");
//使用Android平臺
if (Build.VERSION.SDK_INT != 0) {
return new Android();
}
} catch (ClassNotFoundException ignored) {
}
//使用Java平臺
return new Platform(true);
}
//獲取默認(rèn)請求適配器列表
List<? extends CallAdapter.Factory> defaultCallAdapterFactories(
@Nullable Executor callbackExecutor) {
DefaultCallAdapterFactory executorFactory = new DefaultCallAdapterFactory(callbackExecutor);
return hasJava8Types
? asList(CompletableFutureCallAdapterFactory.INSTANCE, executorFactory)
: singletonList(executorFactory);
}
//獲取默認(rèn)執(zhí)行器
@Nullable Executor defaultCallbackExecutor() {
return null;
}
//Android平臺
static final class Android extends Platform {
Android() {
super(Build.VERSION.SDK_INT >= 24);
}
@Override public Executor defaultCallbackExecutor() {
return new MainThreadExecutor();
}
static class MainThreadExecutor implements Executor {
//主線程Handler
private final Handler handler = new Handler(Looper.getMainLooper());
@Override public void execute(Runnable r) {
handler.post(r);
}
}
}
可以看到,Android是Platform 的靜態(tài)內(nèi)部類煎楣,并覆蓋了defaultCallbackExecutor方法來云挟,用于返回自己的Executor。而Android的Executor實(shí)現(xiàn)類是MainThreadExecutor转质,其本質(zhì)是封裝了主線程的Handler园欣,來執(zhí)行Runnable ,這表明休蟹,Retrofit是在主線程中執(zhí)行的沸枯。
public static final class Builder {
//封裝baseUrl為HttpUrl對象
public Builder baseUrl(String baseUrl) {
Objects.requireNonNull(baseUrl, "baseUrl == null");
//封裝成HttpUrl日矫,后進(jìn)行檢查
return baseUrl(HttpUrl.get(baseUrl));
}
//對baseUrl進(jìn)行判斷,并設(shè)置到成員變量
public Builder baseUrl(HttpUrl baseUrl) {
Objects.requireNonNull(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;
}
示例代碼中绑榴,我們首先傳入了基地址哪轿,它是必須設(shè)置的,并且字符串最后的“/”不能省略翔怎,否則會拋異常窃诉。
地址設(shè)置完成后,又調(diào)用了addConverterFactory傳入數(shù)據(jù)適配器工廠GsonConverterFactory.create()赤套,
public final class GsonConverterFactory extends Converter.Factory {
//創(chuàng)建方法
public static GsonConverterFactory create() {
return create(new Gson());
}
//傳入Gson 對象
public static GsonConverterFactory create(Gson gson) {
return new GsonConverterFactory(gson);
}
//構(gòu)造方法
private GsonConverterFactory(Gson gson) {
if (gson == null) throw new NullPointerException("gson == null");
this.gson = gson;
}
......
}
這里的邏輯比較簡單飘痛,GsonConverterFactory 只是封裝了 一個Gson對象。緊接著我們調(diào)用addCallAdapterFactory容握,設(shè)置了RxJava2CallAdapterFactory 適配器宣脉。
public final class RxJava2CallAdapterFactory extends CallAdapter.Factory {
public static RxJava2CallAdapterFactory create() {
return new RxJava2CallAdapterFactory(null);
}
private RxJava2CallAdapterFactory(Scheduler scheduler) {
this.scheduler = scheduler;
}
RxJava2CallAdapterFactory 繼承了CallAdapter.Factory ,關(guān)于RxJava的原理將在<<RxJava源碼>>中分析。
最終剔氏,構(gòu)建時傳入的參數(shù)都保留在Builder的成員變量中塑猖,Retrofit 會在buid方法中創(chuàng)建,并將參數(shù)傳遞過去,我們看Builder最后的build方法。
public static final class Builder {
......
public Retrofit build() {
//判斷baseUrl不能為null
if (baseUrl == null) {
throw new IllegalStateException("Base URL required.");
}
//不自己配置OkHttpClient,則new OkHttpClient()
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {
callFactory = new OkHttpClient();
}
//不實(shí)現(xiàn)自己的Executor ,則獲取到MainThreadExecutor
Executor callbackExecutor = this.callbackExecutor;
if (callbackExecutor == null) {
callbackExecutor = platform.defaultCallbackExecutor();
}
// 添加默認(rèn)的請求適配器
List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor));
// 添加數(shù)據(jù)轉(zhuǎn)換工廠
List<Converter.Factory> converterFactories = new ArrayList<>(
1 + this.converterFactories.size() + platform.defaultConverterFactoriesSize());
// 如果沒有設(shè)置數(shù)據(jù)轉(zhuǎn)換工廠則會使用默認(rèn)的BuiltInConverters
converterFactories.add(new BuiltInConverters());
converterFactories.addAll(this.converterFactories);
converterFactories.addAll(platform.defaultConverterFactories());
//構(gòu)建Retrofit
return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
}
Retrofit構(gòu)建完成后搁宾,則調(diào)用create方法獲取接口對象。
public final class Retrofit {
//緩存ServiceMethod
private final Map<Method, ServiceMethod<?>> serviceMethodCache = new ConcurrentHashMap<>();
// OkHttpClient
final okhttp3.Call.Factory callFactory;
//基地址
final HttpUrl baseUrl;
//將響應(yīng)數(shù)據(jù)進(jìn)行轉(zhuǎn)換
final List<Converter.Factory> converterFactories;
// 適配器工廠
final List<CallAdapter.Factory> callAdapterFactories;
// 線程執(zhí)行器
final Executor callbackExecutor;
//是否要立即解析接口方法
final boolean validateEagerly;
Retrofit(okhttp3.Call.Factory callFactory, HttpUrl baseUrl,
List<Converter.Factory> converterFactories, List<CallAdapter.Factory> callAdapterFactories,
@Nullable Executor callbackExecutor, boolean validateEagerly) {
this.callFactory = callFactory;
this.baseUrl = baseUrl;
this.converterFactories = converterFactories; // Copy+unmodifiable at call site.
this.callAdapterFactories = callAdapterFactories; // Copy+unmodifiable at call site.
this.callbackExecutor = callbackExecutor;
this.validateEagerly = validateEagerly;
}
//通過動態(tài)代理創(chuàng)建具體的網(wǎng)絡(luò)請求實(shí)體類
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 {
// 如果是Object中的方法,則正常執(zhí)行
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
//如果是platform默認(rèn)的方法則執(zhí)行platform的方法
if (platform.isDefaultMethod(method)) {
return platform.invokeDefaultMethod(method, service, proxy, args);
}
//獲取ServiceMethod執(zhí)行invoke
return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
}
});
}
//獲取ServiceMethod
ServiceMethod<?> loadServiceMethod(Method method) {
//從緩存獲取
ServiceMethod<?> result = serviceMethodCache.get(method);
if (result != null) return result;
//緩存不存在,則調(diào)用parseAnnotations創(chuàng)建
synchronized (serviceMethodCache) {
result = serviceMethodCache.get(method);
if (result == null) {
result = ServiceMethod.parseAnnotations(this, method);
serviceMethodCache.put(method, result);
}
}
return result;
}
}
可以看到,Retrofit通過動態(tài)代理返回了接口代理對象彭则,當(dāng)我們調(diào)用接口方法時鳍刷,將會執(zhí)行代理類的invoke方法。
代理類的invoke里俯抖,前兩個判斷用來過濾Object類和Platform 的方法输瓜,我們通常不會調(diào)用到。而是調(diào)用到關(guān)鍵方法loadServiceMethod芬萍,它將根據(jù)接口方法的聲明和返回類型尤揣,獲取到一個ServiceMethod對象,并調(diào)用其invoke方法柬祠,這個ServiceMethod對象對應(yīng)我們接口的返回類型北戏。
Retrofit的成員變量serviceMethodCache以Method為key,value為ServiceMethod漫蛔,對ServiceMethod進(jìn)行緩存嗜愈。因此loadServiceMethod會首先從緩存獲取旧蛾,如果緩存沒有,則通過ServiceMethod的靜態(tài)方法parseAnnotations來構(gòu)建蠕嫁。
abstract class ServiceMethod<T> {
//獲取ServiceMethod
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.");
}
return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);
}
abstract @Nullable T invoke(Object[] args);
}
ServiceMethod是抽象類,它的子類是HttpServiceMethod剃毒。獲取ServiceMethod時病袄,ServiceMethod會先解析接口方法,并封裝成RequestFactory 對象赘阀,再傳遞給HttpServiceMethod子類益缠。因此,我們來看RequestFactory的parseAnnotations方法是如何解析的纤壁。
final class RequestFactory {
//通過Builder創(chuàng)建
static RequestFactory parseAnnotations(Retrofit retrofit, Method method) {
return new Builder(retrofit, method).build();
}
static final class Builder {
......
final Retrofit retrofit;
final Method method;
final Annotation[] methodAnnotations;
final Annotation[][] parameterAnnotationsArray;
final Type[] parameterTypes;
......
Builder(Retrofit retrofit, Method method) {
this.retrofit = retrofit;
this.method = method;
//獲取注解
this.methodAnnotations = method.getAnnotations();
//獲取形參
this.parameterTypes = method.getGenericParameterTypes();
//獲取形參的注解
this.parameterAnnotationsArray = method.getParameterAnnotations();
}
RequestFactory build() {
//遍歷解析注解
for (Annotation annotation : methodAnnotations) {
parseMethodAnnotation(annotation);
}
//請求方法不能為空
if (httpMethod == null) {
throw methodError(method, "HTTP method annotation is required (e.g., @GET, @POST, etc.).");
}
......
//解析形參注解參數(shù)
int parameterCount = parameterAnnotationsArray.length;
parameterHandlers = new ParameterHandler<?>[parameterCount];
for (int p = 0, lastParameter = parameterCount - 1; p < parameterCount; p++) {
parameterHandlers[p] =
parseParameter(p, parameterTypes[p], parameterAnnotationsArray[p], p == lastParameter);
}
......
//構(gòu)建RequestFactory
return new RequestFactory(this);
}
通過對接口方法的注解左刽、形參和形參注解的解析,RequestFactory對象便持有了接口聲明的各參數(shù)酌媒,它們都以數(shù)組的形式保存了下來欠痴。
緊接著通過執(zhí)行HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory)方法,RequestFactory將被傳遞到ServiceMethod的子類HttpServiceMethod中秒咨。
abstract class HttpServiceMethod<ResponseT, ReturnT> extends ServiceMethod<ReturnT> {
......
static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations(
Retrofit retrofit, Method method, RequestFactory requestFactory) {
boolean isKotlinSuspendFunction = requestFactory.isKotlinSuspendFunction;
boolean continuationWantsResponse = false;
boolean continuationBodyNullable = false;
Annotation[] annotations = method.getAnnotations();
Type adapterType;
if (isKotlinSuspendFunction) {
Type[] parameterTypes = method.getGenericParameterTypes();
Type responseType = Utils.getParameterLowerBound(0,
(ParameterizedType) parameterTypes[parameterTypes.length - 1]);
if (getRawType(responseType) == Response.class && responseType instanceof ParameterizedType) {
responseType = Utils.getParameterUpperBound(0, (ParameterizedType) responseType);
continuationWantsResponse = true;
} else {
}
adapterType = new Utils.ParameterizedTypeImpl(null, Call.class, responseType);
annotations = SkipCallbackExecutorImpl.ensurePresent(annotations);
} else {
adapterType = method.getGenericReturnType();
}
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>)");
}
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) {
return new CallAdapted<>(requestFactory, callFactory, responseConverter, callAdapter);
} else if (continuationWantsResponse) {
return (HttpServiceMethod<ResponseT, ReturnT>) new SuspendForResponse<>(requestFactory,
callFactory, responseConverter, (CallAdapter<ResponseT, Call<ResponseT>>) callAdapter);
} else {
return (HttpServiceMethod<ResponseT, ReturnT>) new SuspendForBody<>(requestFactory,
callFactory, responseConverter, (CallAdapter<ResponseT, Call<ResponseT>>) callAdapter,
continuationBodyNullable);
}
}
@Override final @Nullable ReturnT invoke(Object[] args) {
//創(chuàng)建OkHttpCall
Call<ResponseT> call = new OkHttpCall<>(requestFactory, args, callFactory, responseConverter);
return adapt(call, args);
}
protected abstract @Nullable ReturnT adapt(Call<ResponseT> call, Object[] args);
static final class CallAdapted<ResponseT, ReturnT> extends HttpServiceMethod<ResponseT, ReturnT> {
private final CallAdapter<ResponseT, ReturnT> callAdapter;
CallAdapted(RequestFactory requestFactory, okhttp3.Call.Factory callFactory,
Converter<ResponseBody, ResponseT> responseConverter,
CallAdapter<ResponseT, ReturnT> callAdapter) {
super(requestFactory, callFactory, responseConverter);
this.callAdapter = callAdapter;
}
@Override protected ReturnT adapt(Call<ResponseT> call, Object[] args) {
return callAdapter.adapt(call);
}
}
static final class SuspendForResponse<ResponseT> extends HttpServiceMethod<ResponseT, Object> {
private final CallAdapter<ResponseT, Call<ResponseT>> callAdapter;
SuspendForResponse(RequestFactory requestFactory, okhttp3.Call.Factory callFactory,
Converter<ResponseBody, ResponseT> responseConverter,
CallAdapter<ResponseT, Call<ResponseT>> callAdapter) {
super(requestFactory, callFactory, responseConverter);
this.callAdapter = callAdapter;
}
@Override protected Object adapt(Call<ResponseT> call, Object[] args) {
call = callAdapter.adapt(call);
Continuation<Response<ResponseT>> continuation =
(Continuation<Response<ResponseT>>) args[args.length - 1];
try {
return KotlinExtensions.awaitResponse(call, continuation);
} catch (Exception e) {
return KotlinExtensions.suspendAndThrow(e, continuation);
}
}
}
static final class SuspendForBody<ResponseT> extends HttpServiceMethod<ResponseT, Object> {
private final CallAdapter<ResponseT, Call<ResponseT>> callAdapter;
private final boolean isNullable;
SuspendForBody(RequestFactory requestFactory, okhttp3.Call.Factory callFactory,
Converter<ResponseBody, ResponseT> responseConverter,
CallAdapter<ResponseT, Call<ResponseT>> callAdapter, boolean isNullable) {
super(requestFactory, callFactory, responseConverter);
this.callAdapter = callAdapter;
this.isNullable = isNullable;
}
@Override protected Object adapt(Call<ResponseT> call, Object[] args) {
call = callAdapter.adapt(call);
Continuation<ResponseT> continuation = (Continuation<ResponseT>) args[args.length - 1];
try {
return isNullable
? KotlinExtensions.awaitNullable(call, continuation)
: KotlinExtensions.await(call, continuation);
} catch (Exception e) {
return KotlinExtensions.suspendAndThrow(e, continuation);
}
}
}
......
}
HttpServiceMethod也是抽象類喇辽,它增加了adapt抽象方法。由CallAdapted雨席、SuspendForResponse菩咨、SuspendForBody這3個內(nèi)部類繼承。
由繼承關(guān)系知道陡厘,ServiceMethod的invoke抽米,實(shí)際由HttpServiceMethod來實(shí)現(xiàn),它會先創(chuàng)建了一個OkHttpCall糙置,上面提到它是一個裝飾類云茸,是對RealCall的封裝。
final class OkHttpCall<T> implements Call<T> {
//接口參數(shù)對象
private final RequestFactory requestFactory;
//接口參數(shù)
private final Object[] args;
//OKHttpClient
private final okhttp3.Call.Factory callFactory;
//GsonConverterFactory
private final Converter<ResponseBody, T> responseConverter;
//是否取消請求
private volatile boolean canceled;
//原生call谤饭,即RealCall
@GuardedBy("this")
private @Nullable okhttp3.Call rawCall;
@GuardedBy("this")
private @Nullable Throwable creationFailure;
@GuardedBy("this")
private boolean executed;
OkHttpCall(RequestFactory requestFactory, Object[] args,
okhttp3.Call.Factory callFactory, Converter<ResponseBody, T> responseConverter) {
this.requestFactory = requestFactory;
this.args = args;
this.callFactory = callFactory;
this.responseConverter = responseConverter;
}
由上面分析知道标捺,最終請求調(diào)用是通過OkHttp的RealCall來操作的,我們先來看同步請求的調(diào)用流程揉抵。
final class OkHttpCall<T> implements Call<T> {
......
//同步請求
@Override public Response<T> execute() throws IOException {
okhttp3.Call call;
synchronized (this) {
if (executed) throw new IllegalStateException("Already executed.");
//將executed置為true
executed = true;
......
//首次調(diào)用rawCall為null
call = rawCall;
if (call == null) {
try {
//獲取RealCall
call = rawCall = createRawCall();
} catch (IOException | RuntimeException | Error e) {
throwIfFatal(e);
creationFailure = e;
throw e;
}
}
}
//是否取消
if (canceled) {
call.cancel();
}
//解析ok返回的數(shù)據(jù)
return parseResponse(call.execute());
}
//創(chuàng)建RealCall
private okhttp3.Call createRawCall() throws IOException {
//通過OKHttpClient創(chuàng)建RealCall
okhttp3.Call call = callFactory.newCall(requestFactory.create(args));
if (call == null) {
throw new NullPointerException("Call.Factory returned null.");
}
return call;
}
//解析響應(yīng)數(shù)據(jù)
Response<T> parseResponse(okhttp3.Response rawResponse) throws IOException {
ResponseBody rawBody = rawResponse.body();
......
ExceptionCatchingResponseBody catchingBody = new ExceptionCatchingResponseBody(rawBody);
try {
//根據(jù)設(shè)置的數(shù)據(jù)解析器進(jìn)行解析
T body = responseConverter.convert(catchingBody);
return Response.success(body, rawResponse);
} catch (RuntimeException e) {
catchingBody.throwIfCaught();
throw e;
}
}
通過OKHttpClient創(chuàng)建RealCall亡容,來執(zhí)行網(wǎng)絡(luò)請求,這一部分的具體執(zhí)行流程已在<<OkHttp源碼>>做了分析冤今。而當(dāng)最后一步的parseResponse方法被調(diào)用闺兢,OkHttpCall會根據(jù)我們傳入的數(shù)據(jù)解析器,將響應(yīng)的網(wǎng)絡(luò)數(shù)據(jù)進(jìn)行解析戏罢,并返回給我們的上層調(diào)用列敲。再來看異步請求調(diào)用流程阱佛。
final class OkHttpCall<T> implements Call<T> {
......
//異步請求
@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 {
//獲取RealCall
call = rawCall = createRawCall();
} catch (Throwable t) {
throwIfFatal(t);
failure = creationFailure = t;
}
}
}
if (failure != null) {
callback.onFailure(this, failure);
return;
}
if (canceled) {
call.cancel();
}
//開始異步請求
call.enqueue(new okhttp3.Callback() {
//響應(yīng)回調(diào)
@Override public void onResponse(okhttp3.Call call, okhttp3.Response rawResponse) {
Response<T> response;
try {
//解析響應(yīng)數(shù)據(jù)
response = parseResponse(rawResponse);
} catch (Throwable e) {
throwIfFatal(e);
callFailure(e);
return;
}
try {
//回調(diào)給客戶端
callback.onResponse(OkHttpCall.this, response);
} catch (Throwable t) {
throwIfFatal(t);
t.printStackTrace();
}
}
//失敗回調(diào)
@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
}
}
});
}
異步請求和同步請求的流程大致相同,最終通過接口回調(diào)給上層應(yīng)用戴而。