Retrofit最详细的源码分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shayubuhuifei/article/details/84870169

前言部分

Retrofit是时下最流行的网络请求库,强大的功能为我们的开发工作提供了极大的便利,所以了解其中的大概流程也是十分必要。

建造者模式来构建Retrofit的实例,并进行配置。动态代理来调用service中的方法。

由于能力有限,我只整理了自己分析学习的过程,如有问题或有更好的文章欢迎推荐给我,非常感谢。

正式内容

//在使用方法中开始分析
Retrofit build = new Retrofit
    .Builder()
    .addConverterFactory(GsonConverterFactory.create())
    .baseUrl(ApiService.BASE_API)
    .build();
    
    //调用接口
    ApiService  apiService = build.create(ApiService.class);
    ApiService  call = apiService.getBanner();
    //发起请求获取结果
    call.enqueue(new Callback<bean>() {
            @Override
            public void onResponse(Call<bean> call, Response<bean> response) {
            }
            @Override
            public void onFailure(Call<bean> call, Throwable t){
            }
        });

源码的内部细节实现:

  1. new Retrofit.Builder()方法,如下:
    public Builder() {
    //调用Platform的get()
      this(Platform.get());
    }

  1. Platform 是平台相关,主要区分当前处于什么平台下,如Android、java
//实际是调用findPlatform的方法
 private static final Platform PLATFORM = findPlatform();

  static Platform get() {
    return PLATFORM;
  }
  
  private static Platform findPlatform() {
    try {
      Class.forName("android.os.Build");
      if (Build.VERSION.SDK_INT != 0) {
        return new Android();
      }
    } catch (ClassNotFoundException ignored) {
    }
    try {
      Class.forName("java.util.Optional");
      return new Java8();
    } catch (ClassNotFoundException ignored) {
    }
    return new Platform();
  }
  1. 调用baseurl()传入string,内部是调用额另外一个重载方法
 public Builder baseUrl(String baseUrl) {
      checkNotNull(baseUrl, "baseUrl == null");
      return baseUrl(HttpUrl.get(baseUrl));
    }
    
  public Builder baseUrl(HttpUrl baseUrl) {
      checkNotNull(baseUrl, "baseUrl == null");
      List<String> pathSegments = baseUrl.pathSegments();
      if (!"".equals(pathSegments.get(pathSegments.size() - 1))) {
        throw new IllegalArgumentException("baseUrl must end in /: " + baseUrl);
      }
      this.baseUrl = baseUrl;
      return this;
    }
  1. 调用addConverterFactory方法,传入转换器工厂。如我们传入的gosn
//这里主要就是加入到了转换器工厂的ArrayList中存储,后续使用
  public Builder addConverterFactory(Converter.Factory factory) {
      converterFactories.add(checkNotNull(factory, "factory == null"));
      return this;
    }
  1. 再调用最后一个build方法完成构建工作。
public Retrofit build() {
      if (baseUrl == null) {
        throw new IllegalStateException("Base URL required.");
      }
    //这个factory其实就是OkHttpClient,这里可以通过client方法传入,如果没有传入就new一个出来
      okhttp3.Call.Factory callFactory = this.callFactory;
      if (callFactory == null) {
        callFactory = new OkHttpClient();
      }

      Executor callbackExecutor = this.callbackExecutor;
      if (callbackExecutor == null) {
      //调用这个切换主线程使用
        callbackExecutor = platform.defaultCallbackExecutor();
      }

      // Make a defensive copy of the adapters and add the default Call adapter.
      //call 的适配器主要是对call的转换,下面介绍call的时候在解释
      List<CallAdapter.Factory> callAdapterFactories = new ArrayList<>(this.callAdapterFactories);
      callAdapterFactories.addAll(platform.defaultCallAdapterFactories(callbackExecutor));

      // Make a defensive copy of the converters.
      //我们自己加入的支持的对象转换(如json,string)
      List<Converter.Factory> converterFactories = new ArrayList<>(
          1 + this.converterFactories.size() + platform.defaultConverterFactoriesSize());

      // Add the built-in converter factory first. This prevents overriding its behavior but also
      // ensures correct behavior when using converters that consume all types.
      converterFactories.add(new BuiltInConverters());
      converterFactories.addAll(this.converterFactories);
      converterFactories.addAll(platform.defaultConverterFactories());
    //处理完上面一堆准备工作,返回一个可用的Retrofit。
      return new Retrofit(callFactory, baseUrl, unmodifiableList(converterFactories),
          unmodifiableList(callAdapterFactories), callbackExecutor, validateEagerly);
    }

通过构建者来准备必要的基础类,这里开始看Retrofit的后续方法。

 IpService ipService = retrofit.create(IpService.class);//1
 Call<IpModel>call=ipService.getIpMsg(ip);//2
 

先看上面的方法调用create方法,返回一个newProxyInstance代理类。调用我们传入类的方法也是通过代理对象来实现调用。(动态代理模式的好处请自行查看)

 @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();
          private final Object[] emptyArgs = new Object[0];
//我们调用service中的方法,实际是会调用到这个invoke方法。参数分别是代理对象,方法,参数。
          @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);
            }
            //上面是通过反射来实际调用我们传入的service中的方法,下面loadServiceMethod,看如何加载我们传入类中的方法
            return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);
          }
        });
  }

下面这个方法是loadServiceMethod,看一下内容不多,主要是去缓存中查询,查不到再去其他地方找,找到后在缓存上,可见如果我们请求过的接口,在次请求会直至在缓存中查询方法。加快构建一个请求的速度。

 ServiceMethod<?> loadServiceMethod(Method method) {
    //这里有一个缓存的方法集合,如果能查询到就直接返回该方法了。
    ServiceMethod<?> result = serviceMethodCache.get(method);
    if (result != null) return result;

    synchronized (serviceMethodCache) {
      result = serviceMethodCache.get(method);
      if (result == null) {
      //没查询到的话调用下面方法,查询完成后,把方法在存入到缓存中去了。
        result = ServiceMethod.parseAnnotations(this, method);
        serviceMethodCache.put(method, result);
      }
    }
    return result;
  }

ServiceMethod.parseAnnotations方法中校验一些规则,如:方法返回值不能为void等,最后在调用 return HttpServiceMethod.parseAnnotations(retrofit, method, requestFactory);方法。获取一个HttpServiceMethod对象。

  static <ResponseT, ReturnT> HttpServiceMethod<ResponseT, ReturnT> parseAnnotations(
      Retrofit retrofit, Method method, RequestFactory requestFactory) {
     //创建createCallAdapter,方法内包含获取方法上注解,参数等逻辑,并且回掉到 
     //retrofit.callAdapter(returnType, annotations);方法。加入到adapter列表中
    CallAdapter<ResponseT, ReturnT> callAdapter = createCallAdapter(retrofit, method);
    Type responseType = callAdapter.responseType();
    if (responseType == Response.class || responseType == okhttp3.Response.class) {
      throw methodError(method, "'"
          + Utils.getRawType(responseType).getName()
          + "' is not a valid response body type. Did you mean ResponseBody?");
    }
    if (requestFactory.httpMethod.equals("HEAD") && !Void.class.equals(responseType)) {
      throw methodError(method, "HEAD method must use Void as response type.");
    }
    //创建请求体的转换器ResponseBody可转换为多种类型
    Converter<ResponseBody, ResponseT> responseConverter =
        createResponseConverter(retrofit, method, responseType);
    //使用retrofit中的okhttpclient
    okhttp3.Call.Factory callFactory = retrofit.callFactory;
    //创建HttpServiceMethod网络服务方法对象
    return new HttpServiceMethod<>(requestFactory, callFactory, callAdapter, responseConverter);
  }

上面操作完成后会调用loadServiceMethod后我们会得到一个返回值,类型就是HttpServiceMethod,然后在调用该类中的invoke方法。

return loadServiceMethod(method).invoke(args != null ? args : emptyArgs);

invoke中代码比较简单也很关键,这里创建了OkHttpCall类并传入一些参数,这个地方有点绕,调用如下:

@Override ReturnT invoke(Object[] args) {
    return callAdapter.adapt(
        new OkHttpCall<>(requestFactory, args, callFactory, responseConverter));
  }

调用adapte方法传入一个OkHttpCall类,adapte会调用ExecutorCallAdapterFactory的adapte(),在ExecutorCallAdapterFactory类中创建内部类ExecutorCallbackCall类(Call的子类)。

以上这个create方法基本完成

说完这里我们在回去主线中去调用了creat方法后,进行复杂的操作后,下面开始正式的调用service中的方法。就是我们上面的getIpMsg()方法拿到返回的call对象(Call子类),然后在调用enqueue方法发起网络请求(execute同步方法),下面看一下Call里面的enqueue方法。
这里其实调用ExecutorCallbackCall中的enqueue方法,代码如下:

   @Override public void enqueue(final Callback<T> callback) {
      checkNotNull(callback, "callback == null");
      
        //delegate其实是我们传入的OkHttpCall类
      delegate.enqueue(new Callback<T>() {
        @Override public void onResponse(Call<T> call, final Response<T> response) {
        //初始化是在retrofit中的build中,一直往前找发现callbackExecutor是MainThreadExecutor,其实就是切换到了主线程中
          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);
              }
            }
          });
        }

        @Override public void onFailure(Call<T> call, final Throwable t) {
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              callback.onFailure(ExecutorCallbackCall.this, t);
            }
          });
        }
      });
    }

下面我们看一下OkHttpCall中的enqueue方法,这个方法里面比较主要的内容有两个,一个使用okhttp3.Call来获取真实的网络返回数据并关联起来我们封装的call中;另一个是数据格式的转换

 @Override public void enqueue(final Callback<T> callback) {
    checkNotNull(callback, "callback == null");

    okhttp3.Call call;
    Throwable failure;

    synchronized (this) {
      if (executed) throw new IllegalStateException("Already executed.");
      executed = true;

      call = rawCall;
      failure = creationFailure;
      if (call == null && failure == null) {
        try {
        //如果为null就创建一个okhttp3.Call
          call = rawCall = createRawCall();
        } catch (Throwable t) {
          throwIfFatal(t);
          failure = creationFailure = t;
        }
      }
    }

    if (failure != null) {
      callback.onFailure(this, failure);
      return;
    }

    if (canceled) {
      call.cancel();
    }
    //call的enqueue方法调用
    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 {
        //okhttp的结果给到我们传入的callback
          callback.onResponse(OkHttpCall.this, response);
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }

      @Override public void onFailure(okhttp3.Call call, IOException e) {
        callFailure(e);
      }

      private void callFailure(Throwable e) {
        try {
          callback.onFailure(OkHttpCall.this, e);
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
    });
  }

上面基本上已经将okhttp的call和我们传入的call关联一起,事实上我们已经拿到网络请求的结果了。

最后在看一下转换的方法,这里应该就是我们配置的转换器了,比如gosn转换器还有很多种。

 Response<T> parseResponse(okhttp3.Response rawResponse) throws IOException {
    ResponseBody rawBody = rawResponse.body();

    // Remove the body's source (the only stateful object) so we can pass the response along.
    rawResponse = rawResponse.newBuilder()
        .body(new NoContentResponseBody(rawBody.contentType(), rawBody.contentLength()))
        .build();
//根据不同的code码来不同的处理逻辑
    int code = rawResponse.code();
    if (code < 200 || code >= 300) {
      try {
        // Buffer the entire body to avoid future I/O.
        ResponseBody bufferedBody = Utils.buffer(rawBody);
        return Response.error(bufferedBody, rawResponse);
      } finally {
        rawBody.close();
      }
    }

    if (code == 204 || code == 205) {
      rawBody.close();
      return Response.success(null, rawResponse);
    }

    ExceptionCatchingResponseBody catchingBody = new ExceptionCatchingResponseBody(rawBody);
    try {
    //进行转换Converter是其他各种转换器的父类,这里根据你设置的转换器进行转换了
      T body = responseConverter.convert(catchingBody);
      return Response.success(body, rawResponse);
    } catch (RuntimeException e) {
      // If the underlying source threw an exception, propagate that rather than indicating it was
      // a runtime exception.
      catchingBody.throwIfCaught();
      throw e;
    }
  }

以上基本上是我理解的整个流程。

有问题欢迎纠正,谢谢啦

如果对你有帮助就点个赞把,你的鼓励是我前进的动力。

猜你喜欢

转载自blog.csdn.net/shayubuhuifei/article/details/84870169