http://blog.youkuaiyun.com/guiman/article/details/51480497
http://www.jianshu.com/p/097947afddaf
主要的类:
Retrofit:总类 负责各模块的组装
ServiceMethod:将注解转为http请求参数
CallAdapter:适配器类 转换为具体的Call的子类 执行网络请求操作
Converter:对返回的结果进行处理
二、调用步骤
//创建Retrofit对象
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
//动态生成一个代理对象
GitHub github = retrofit.create(GitHub.class);
//生成一个OKHttpCall的代理对象
final retrofit2.Call<ResponseBody> call = github.contributors("square", "retrofit");
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
System.out.println("----->"+response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
2.1 Retrofit.build
会创建:
callFactory = new OkHttpClient();
//用于回调到主线程 在android中为Handler
callbackExecutor = platform.defaultCallbackExecutor();
//创建ExecutorCallAdapterFactory对象
adapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));
2.2 retrofit.create
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() {
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 会对注解进行解析
ServiceMethod serviceMethod = loadServiceMethod(method);
//创建OkHttpCall 用于执行实际的网络请求
OkHttpCall okHttpCall = new OkHttpCall<>(serviceMethod, args);
//将okHttpCall传入ExecutorCallAdapterFactory.ExecutorCallbackCall 并返回
return serviceMethod.callAdapter.adapt(okHttpCall);
}
});
}
loadServiceMethod会调用ServiceMethod.build
在其中会调用createCallAdapter获取ExecutorCallAdapterFactory的实例
调用createResponseConverter获取BuiltInConverters的实例
重点是创建OkHttpCall,并传入ExecutorCallAdapterFactory.ExecutorCallbackCall。 会在里面执行实际的网络请求
2.3 call.enqueue(new Callback<ResponseBody>(){})
call是通过Retrofit.create获得的ExecutorCallAdapterFactory.ExecutorCallbackCall的实例,
执行call.enqueue将执行
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");
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()) {
// Emulate OkHttp's behavior of throwing/delivering an IOException on cancellation.
callback.onFailure(ExecutorCallbackCall.this, new IOException("Canceled"));
} else {
callback.onResponse(ExecutorCallbackCall.this, response);
}
}
});
}
delegate为create方法中传入的OkHttpCall实例,callbackExecutor为Retrofit.build中创建的Handler对象
2.4 返回结果
最终的执行在OKHttpCall.java的enqueue方法中
call.enqueue(new okhttp3.Callback() {
@Override public void onResponse(okhttp3.Call call, okhttp3.Response rawResponse)
throws IOException {
Response<T> response;
try {
response = parseResponse(rawResponse);
} catch (Throwable e) {
callFailure(e);
return;
}
callSuccess(response);
}
调用parseResponse方法,实际执行Converter的responseBodyConverter方法解析返回结果,并最终使用Handler返回