最近用到Retrofit,发现只需要定义一个接口就能完成相应功能,这是怎么做到的?
这是用到了java的动态代理机制,Proxy类
还是先来看下使用的流程吧,和okhttp一样分为异步和同步,其实Retrofit是对网络请求接口的封装,具体的网络请求是通过底层的Okhttp来实现的,所以后面的流程是一样的
先定义一个接口
public interface IRetrofitTest {
@GET
Call<String> get(@Url String url);
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://www.baidu.com/")
.addConverterFactory(ScalarsConverterFactory.create())
.build();
IRetrofitTest iRetrofitTest = retrofit.create(IRetrofitTest.class);
Call<String> call = iRetrofitTest.get("http://www.baidu.com");
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
Log.d("Response", response.body());
}
@Override
public void onFailure(Call<String> call, Throwable t) {
}
});
以上便完成了异步网络请求。
可以看到,Call call = iRetrofitTest.get(“http://www.baidu.com”);
Call接口是怎么被实现的?带着这些疑问,来看看源码
public <T> T create(final Class<T> service) {
Utils.validateServiceInterface(service);
if (validateEagerly) {
eagerlyValidateMethods(service);
}
//Proxy是java实现动态代理的类
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 {
// If the method is a method from Object then defer to normal invocation.
//如果是Object类,直接调用里面的方法
if (method.getDeclaringClass() == Object.class) {
return method.invoke(this, args);
}
//如果是default方法,则直接调用改默认方法
if (platform.isDefaultMethod(method)) {
return platform.invokeDefaultMethod