Retrofit使用者会觉得接口+注解方式去写网络请求很吊。但当你真正的去看它的源码,
会被它独特,漂亮的解耦方式所吸引,整个结构运用了动态代理,策略模式,Builder模式,工厂等设计模式。
Retrofit v2.1基于Okhttp3,可以说是对okhttp进行二次封装。
先感受一下使用方式。。。。
Retrofit官方使用方式
//定义HTTP API接口
public interface LocationService {
@Headers({
"Accept: application/json",
"Content-Type: application/json"
})
@GET("geo/ip?")
Call<Location> currentLocation();
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
LocationService service = retrofit.create(LocationService.class);
retrofit2.Call<Location> call = service.currentLocation();
call.enqueue(new retrofit2.Callback<Location>() {
@Override
public void onResponse(retrofit2.Call<Location> call, retrofit2.Response<Location> response) {
Log.d(TAG, "onResponse: " + response.body());
}
@Override
public void onFailure(retrofit2.Call<Location> call, Throwable t) {
Log.d(TAG, "onFailure: " + t.getMessage());
}
});
Retrofit + RxJava 的使用方式
public interface RxLocationService {
@Headers({
"Accept: application/json",
"Content-Type: application/json"
})
@GET("geo/ip?)
Observable<Location> currentLocation();
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
RxLocationService service = retrofit.create(RxLocationService.class);
Observable<Location> ob = service.currentLocation();
ob.subscribeOn(Schedulers.newThread())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Location>() {
@Override
public void onCompleted() {
Log.d(TAG, "onCompleted: ");
}
@Override
public void on Error(Throwable e) {
Log.d(TAG, "on Error: ");
}
@Override
public void onNext(Location location) {
Log.d(TAG, "onNext: " + location);
}
});
Retrofit的配置
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.callFactory(new OkHttpClient())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
通过addCallAdapterFactory和addConverterFactory配置不同的OkHttpClient,
CallAdapter(调用方式的适配器),Converter(数据转换器)。
下面时本人分析的流程图:
CallAdapter
接口
这个接口决定HTTP API接口里每个方法的返回值类型。
Retrofit的create方法
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)) { // java8的默认方法
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);
}
});
运用了动态代理生成API接口的实现,动态代理会有个invoke方法,当调用接口的一个方法时会被调用。
例如:上面使用方式提到的LocationService,当调用locationService.currentLocation()时,invoke方法
会被调用,返回值就是Call
ServiceMethod:存储了API接口的需要的注解,包含request mothod, request header等信息。
loadServiceMethod(method)整个方法解析当前方法的注解等属性。
OkHttpCall对okhttp进行了封装。传入ServiceMethod,args进行OkHttpCall实例化,
最终调用serviceMethod.callAdapter.adapt(okHttpCall) 产生CallAdapter.
public interface CallAdapter<R, T>{}
Type responseType();
<R> T adapt(Call<R> call);
abstract class Factory {
public abstract CallAdapter<?> get(Type returnType, Annotation[] annotations,
Retrofit retrofit);
protected static Type getParameterUpperBound(int index, ParameterizedType type) {
return Utils.getParameterUpperBound(index, type);
}
protected static Class<?> getRawType(Type type) {
return Utils.getRawType(type);
}
}
}
CallAdapter的作用:
1.当调用API接口一个方法时就会产生一个CallAdapter实现类;
2.adapt方法返回的T决定API接口方法的返回类型;(对应例子currentLocation返回值Call类型)
3.responseType方法返回的Type时网络请求返回类型。(对应Call上面泛型Location的类型)
在Retrofit的配置里讲到,可以配置多种CallAdapter,就可以实现API接口返回不同的类型,从而可以又不同调用方式。
也就产生上面提到的 Retrofit官方使用方式 和 Retrofit + RxJava 的使用方式。
官方使用方式的实现在ExecutorCallAdapterFactory.java, ExecutorCallAdapterFactory是在Retrofit的build方法配置了默认的CallAdapterFactory。
//在ExecutorCallAdapterFactory.java
return new CallAdapter<Object, Call<?>>() {
@Override public Type responseType() {
return responseType;
}
@Override public Call<Object> adapt(Call<Object> call) {
return new ExecutorCallbackCall<>(callbackExecutor, call);
}
};
//adapt的方法new ExecutorCallbackCall<>(callbackExecutor, call)
//在ExecutorCallbackCall.java
// 上面官方使用方式例子里调用如下:
// retrofit2.Call<Location> call = service.currentLocation();
// call.enqueue(callback);
@Override public void enqueue(final Callback<T> callback) {
if (callback == null) throw new NullPointerException("callback == null");
delegate.enqueue(new Callback<T>() { //delegate 实质是OkHttpCalls
@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);
}
}
});
}
}
...
}
OkHttpCall:
1. Response<T> execute() // 同步
2. void enqueue(final Callback<T> callback) // 异步
```sh
// OkHttpCall的关键代码
private okhttp3.Call createRawCall() throws IOException {
Request request = serviceMethod.toRequest(args);
okhttp3.Call call = serviceMethod.callFactory.newCall(request);
if (call == null) {
throw new NullPointerException("Call.Factory returned null.");
}
return call;
}
Response<T> parseResponse(okhttp3.Response rawResponse) throws IOException {
...
try {
T body = serviceMethod.toResponse(catchingBody);
return Response.success(body, rawResponse);
} catch (RuntimeException e) {
...
}
//ServiceMethod的toResponse方法实现
R toResponse(ResponseBody body) throws IOException {
return responseConverter.convert(body);
}
```
通过ServiceMethod的toRequest(args)方法转成okhttp的Request, 然后通过okhtpp去完成网络请求。
返回结果通过toResponse(catchingBody)交给Converter,最终转成T调回。
Retrofit + RxJava 的使用方式实现在RxJavaCallAdapterFactory.java。这里不产开表述了。
Converter接口
Retrofit默认配置里没有配置ConverterFactory, 初次使用时通常忘了配置一个ConverterFactory导致出错。
Retrofit以插件的形式提供了多种Converter实现:
- Gson: com.squareup.retrofit2:converter-gson
- Jackson: com.squareup.retrofit2:converter-jackson
- Moshi: com.squareup.retrofit2:converter-moshi
- Protobuf: com.squareup.retrofit2:converter-protobuf
- Wire: com.squareup.retrofit2:converter-wire
- Simple XML: com.squareup.retrofit2:converter-simplexml
- Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars