本篇承接 Retrofit简单使用
Retrofit底层还是okhttp3,但是对比okhttp3简单实用及源码分析(一)可以看出来,retrofit不需要进行okhttp3的配置,并且retrofit引入了GsonConverterFactory,简化了json解析过程,具体的下面分析。
先看流程图:
以下是关键代码节点:
1. 首先初始化RequestFactory、Converter、calladapter:
public <T> T create(final Class<T> service) {
...
//验证是否为默认方法,如果不是默认方法会添加到缓存map集合中,默认false
if (validateEagerly) {
eagerlyValidateMethods(service);
}
//反射形式返回接口的实例化对象
return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
new InvocationHandler() {
...
//一般正常返回的位置
return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
}
});
}
...
ServiceMethod<?> loadServiceMethod(Method method) {
...
synchronized (serviceMethodCache) {
result = serviceMethodCache.get(method);
if (result == null) {
//获取ServiceMethod对象
result = ServiceMethod.parseAnnotations(this, method);
serviceMethodCache.put(method, result);
}
}
return result;
}
...
static <T> ServiceMethod<T> parseAnnotations(Retrofit retrofit, Method method) {
//获取请求Factory对象
RequestFactory requestFactory = RequestFactory.parseAnnotations(retrofit, method);
...
//生成ServiceMethod对象
return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);
}
...
//检查接口上的注释,构建可重用的Http服务方法
static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations(
Retrofit retrofit, Method method, RequestFactory requestFactory) {
//将ResponseT转化为ReturnT的适配器
CallAdapter<ResponseT, ReturnT> callAdapter = createCallAdapter(retrofit, method);
...
//将ResponseBody转化为ResponseT的http表现形式转化对象
Converter<ResponseBody, ResponseT> responseConverter =
createResponseConverter(retrofit, method, responseType);
...
}
2. 根据上面配置生成HttpServiceMethod对象:
static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations(
Retrofit retrofit, Method method, RequestFactory requestFactory) {
...
okhttp3.Call.Factory callFactory = retrofit.callFactory;
return new HttpServiceMethod<>(requestFactory, callFactory, callAdapter, responseConverter);
}
3. HttpServiceMethod中调用invoke的时候生成okhttpcall对象:
@Override ReturnT invoke(Object[] args) {
return callAdapter.adapt(
new OkHttpCall<>(requestFactory, args, callFactory, responseConverter));
}
4. 调用RequestFactory 的 create方法创建请求信息:
private okhttp3.Call createRawCall() throws IOException {
okhttp3.Call call = callFactory.newCall(requestFactory.create(args));
...
}
5. okhttpcall对象中创建okhttp3回调:
@Override public void enqueue(final Callback<T> callback) {
checkNotNull(callback, "callback == null");
okhttp3.Call call;
...
//调用 okhttp3.Call的call.enqueue方法
call.enqueue(new okhttp3.Callback() {
...
});
}
6. 获取请求结果通过Converter转化为接口指定对象:
@Override public void enqueue(final Callback<T> callback) {
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();
}
}
...
});
}