美文网首页
Retrofit源码探究

Retrofit源码探究

作者: rockman_ | 来源:发表于2017-06-06 22:36 被阅读0次

    简要

    原理,将一个Java接口翻译成一个Http请求,然后用Okhttp去发送这个请求

    精华代码

    @SuppressWarnings("unchecked") // Single-interface proxy creation guarded by parameter safety.
      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, @Nullable 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<Object, Object> serviceMethod =
                    (ServiceMethod<Object, Object>) loadServiceMethod(method);
                OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
                return serviceMethod.callAdapter.adapt(okHttpCall);
              }
            });
    }
    
    • Java动态代理:当你调用某个Class的方法前或后,插入你想要执行的代码
    ZhuanLanApi api = retrofit.create(ZhuanLanApi.class);
    
    Call<ZhuanLanAuthor> call = api.getAuthor("qinchao");
    

    这里create方法就是返回了一个Proxy.newProxyInstance动态代理对象

    使用动态原理的原因

    Call<ZhuanLanAuthor> call = api.getAuthor("qinchao");
    

    当api调用getAutor方法时会被动态代理拦截,然后调用Proxy.newProxyInstance方法中的InvocationHandler对象。其中会生成一个对象ServiceMethod,最后返回一个Call对象,Retrofit2中Call接口的默认实现是OkHttpCall,它默认使用OkHttp3作为底层http请求client

    • ServiceMethod就像是一个中央处理器,传入Retrofit对象和Method对象,调用各个接口和解析器,最终生成一个Request,包含api 的域名、path、http请求方法、请求头、是否有body、是否是multipart等等。

    总结

    Retrofit非常巧妙的用注解来描述一个HTTP请求,将一个HTTP请求抽象成一个Java接口,然后用了Java动态代理的方式,动态的将这个接口的注解“翻译”成一个HTTP请求,最后再执行这个HTTP请求

    参考:

    1. Retrofit2 源码解析
    2. retrofit源码

    相关文章

      网友评论

          本文标题:Retrofit源码探究

          本文链接:https://www.haomeiwen.com/subject/eywnfxtx.html