看看Retrofit2.0运行方式

写下这篇你文章,主要是纪念自己的无知和善良,以及要好好学习啊。 都说Retrofit是大Android目前最简单的网络请求框架,和RxJava结合简直就是天人合一,的确,项目中用来一下,的确好用,好玩,也好快(这要靠OkHttp的功劳),写下这个纪念一下Retrofit2.0的工作流程,菜鸟大神勿喷。

首先,gradle配置:

    compile 'io.reactivex:rxjava:1.1.0'
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4'
    compile 'com.squareup.okhttp3:logging-interceptor:3.1.2'

Retrofit工具类:

public static  T createService(Class clazz) {
        if (retrofit == null) {
            synchronized (RetrofitUtil.class) {
                Retrofit.Builder builder = new Retrofit.Builder();
                retrofit = builder.baseUrl(Constant.BASE_IP)
                        .client(okHttpClient)
                        .addConverterFactory(gsonConverterFactory)
                        .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                        .build();
            }
        }
        return retrofit.create(clazz);
    }

列出一个项目的接口:

    /**
     * 获取商品信息
     * @param optionsMap
     * @return
     */
    @GET(Constant.BASE_ACTION)
    Observable getProductInfo(@QueryMap Map optionsMap) ;

然后MVP模式中使用:

        UserRetrofitUtil.
                createService(UserRequestService.class).
                getProductInfo(strParamters).
                observeOn(AndroidSchedulers.mainThread()).
                subscribeOn(Schedulers.io()).subscribe(new Subscriber() {
            @Override
            public void onCompleted() {
                getMvpView().hideLoading();
            }

            @Override
            public void onError(Throwable e) {
                LogUtils.d("upload error..." + e);
                showErrorPage();
            }

            @Override
            public void onNext(ProductDetailBean stateBean) {
                LogUtils.d("upload success" + stateBean);
                getMvpView().setDataFromRemoteSource(stateBean);
            }
        });

好了,现在要看大招了,一点一点去分析Retrofit的运行过程了:
1.看Retrofit#create()方法:

public  T create(final Class 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);
            }
            return loadMethodHandler(method).invoke(args);
          }
        });
  }

放心,去掉一大坨方法,只有loadMethodHandler(method).invoke(args)有用:

 MethodHandler loadMethodHandler(Method method) {
    MethodHandler handler;
    synchronized (methodHandlerCache) {
      handler = methodHandlerCache.get(method);
      if (handler == null) {
        handler = MethodHandler.create(this, method);
        methodHandlerCache.put(method, handler);
      }
    }
    return handler;
  }

终于Retrofit中一个最重要的类MethodHanlder出现了,就因为它我们就可以随便在我们的方法加上注解,就可以进行网络请求了,稍等会深入说明,methodHandlerCache是一个Map类型的容器,loadMethodHandler方法很明显,获得MethodHanlder。

2.MethodHandler类,来看其create方法:

static MethodHandler create(Retrofit retrofit, Method method) {
    CallAdapter callAdapter = createCallAdapter(method, retrofit);
    Type responseType = callAdapter.responseType();
    if (responseType == Response.class || responseType == okhttp3.Response.class) {
      throw Utils.methodError(method, "'"
          + Types.getRawType(responseType).getName()
          + "' is not a valid response body type. Did you mean ResponseBody?");
    }
    Converter<ResponseBody, ?> responseConverter =
        createResponseConverter(method, retrofit, responseType);
    RequestFactory requestFactory = RequestFactoryParser.parse(method, responseType, retrofit);
    return new MethodHandler(retrofit.callFactory(), requestFactory, callAdapter,
        responseConverter);
  }

现在MethodHandler存在四个参数:okhttp3.Call.Factory ,RequestFactory,CallAdapter,Converter。基本上这四个参数值就是Retrofit的精髓了:
2.1 okhttp2.Call.Factory:来源retrofit.callFactory(),好看源码:

//Retrofit2.0类中:
 public okhttp3.Call.Factory callFactory() {
    return callFactory;
  }

在Retrofit#Builder内部类中存在这样一句话:

      **okhttp3.Call.Factory callFactory = this.callFactory;
      if (callFactory == null) {
        callFactory = new OkHttpClient();
      }**

      // Make a defensive copy of the adapters and add the default Call adapter.
      List adapterFactories = new ArrayList<>(this.adapterFactories);
      adapterFactories.add(Platform.get().defaultCallAdapterFactory(callbackExecutor));

      // Make a defensive copy of the converters.
      List converterFactories = new ArrayList<>(this.converterFactories);

      **return new Retrofit(callFactory, baseUrl, converterFactories, adapterFactories,
          callbackExecutor, validateEagerly);**

好了,此时我们Factory就是OkHttpClient了,那么OkHttpClient又是个什么鸟玩意呢? 不急,进去看看吧:
看看Retrofit2.0运行方式_第1张图片
哦哦哦哦哦,对不起啊,去到了Ok3.0的包里面了,好吧我们越界了,现在就简单了解一下这个OkHttpClient应该是关于请求的吧客户端吧(不是叫client吗,需要用到了再去看了吧),参数1到此为止。

2.2 RequestFactory:

RequestFactory requestFactory = RequestFactoryParser.parse(method, responseType, retrofit)

那么现在可以看看这里面写啥吧:

static RequestFactory parse(Method method, Type responseType, Retrofit retrofit) {
    RequestFactoryParser parser = new RequestFactoryParser(method);

    Annotation[] methodAnnotations = method.getAnnotations();
    parser.parseMethodAnnotations(responseType, methodAnnotations);
    parser.parseParameters(retrofit, methodAnnotations);

    return parser.toRequestFactory(retrofit.baseUrl());
  }

比较简洁,但是不简单啊,这5行代码,需要对JAVA的反射知识非常的熟悉,第一次看到了很多方法是我们以前不常用的,这里我也不详细扯蛋了,毕竟这不是重点。
来看parser.parseMethodAnnotations(responseType, methodAnnotations):

private void parseMethodAnnotations(Type responseType, Annotation[] methodAnnotations) {
    for (Annotation annotation : methodAnnotations) {
      if (annotation instanceof DELETE) {
        parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(), false);
      } else if (annotation instanceof GET) {
        parseHttpMethodAndPath("GET", ((GET) annotation).value(), false);
      } 
      ........
      //此处省略n多字
  }

来看看我们熟悉的@GET解析方法:parseHttpMethodAndPath(“GET”, ((GET) annotation).value(), false);

private void parseHttpMethodAndPath(String httpMethod, String value, boolean hasBody) {
    if (this.httpMethod != null) {
      throw methodError(method, "Only one HTTP method is allowed. Found: %s and %s.",
          this.httpMethod, httpMethod);
    }
    this.httpMethod = httpMethod;
    this.hasBody = hasBody;

    if (value.isEmpty()) {
      return;
    }

    // Get the relative URL path and existing query string, if present.
    int question = value.indexOf('?');
    if (question != -1 && question < value.length() - 1) {
      // Ensure the query string does not have any named parameters.
      String queryParams = value.substring(question + 1);
      Matcher queryParamMatcher = PARAM_URL_REGEX.matcher(queryParams);
      if (queryParamMatcher.find()) {
        throw methodError(method, "URL query string \"%s\" must not have replace block. "
            + "For dynamic query parameters use @Query.", queryParams);
      }
    }

    this.relativeUrl = value;
    this.relativeUrlParamNames = parsePathParameters(value);
  }

代码中主要是确定请求方法,HTTP中网络请求除了我们熟悉的GET/POST之外,还有DELETE/HEAD/PATCH/PUT/OPTIONS等等,别问我为什么,我也是看代码才知道的。

来看parser.parseParameters(retrofit, methodAnnotations);

private void parseParameters(Retrofit retrofit, Annotation[] methodAnnotations) {
    Type[] parameterTypes = method.getGenericParameterTypes();
    Annotation[][] parameterAnnotationsArray = method.getParameterAnnotations();

    boolean gotField = false;
    boolean gotPart = false;
    boolean gotBody = false;
    boolean gotPath = false;
    boolean gotQuery = false;
    boolean gotUrl = false;

    int count = parameterAnnotationsArray.length;
    RequestAction[] requestActions = new RequestAction[count];
    for (int i = 0; i < count; i++) {
      Type parameterType = parameterTypes[i];
      if (Utils.hasUnresolvableType(parameterType)) {
        throw parameterError(i, "Parameter type must not include a type variable or wildcard: %s",
            parameterType);
      }

      Annotation[] parameterAnnotations = parameterAnnotationsArray[i];
      if (parameterAnnotations != null) {
        for (Annotation parameterAnnotation : parameterAnnotations) {
          RequestAction action = null;
          if (parameterAnnotation instanceof Url) {
             .....//代码省略n多行
             // 本方法主要是处理网络请求的参数,如下:
         // @GET("/user/{user}/")
         // Call GetUserInfo(@PATH("user") String userId);
         // 使用动态代理,将userid=3的值,直接植入到/user/3/中[3为example]
         // 所以本方法作用就是处理你使用的Retrofit的中相关注解 并将其封装成请求参数
         // 代码很长,不过思路非常清晰,另外还需要一点Http相关的知识,不然有些判断会有点懵
         // @Url @PATH @Query @QuertMap @Header @Field @FieldMap @Part @PartMap @Body
     else if (parameterAnnotation instanceof Path) { 
            if (gotQuery) {
              throw parameterError(i, "A @Path parameter must not come after a @Query.");
            }
            if (gotUrl) {
              throw parameterError(i, "@Path parameters may not be used with @Url.");
            }
            if (relativeUrl == null) {
              throw parameterError(i, "@Path can only be used with relative url on @%s",
                  httpMethod);
            }
            gotPath = true;

            Path path = (Path) parameterAnnotation;
            String name = path.value();
            validatePathName(i, name);

            Converter converter =
                retrofit.stringConverter(parameterType, parameterAnnotations);
            action = new RequestAction.Path<>(name, converter, path.encoded());

          } else if (parameterAnnotation instanceof Query) {
            Query query = (Query) parameterAnnotation;
            String name = query.value();
            boolean encoded = query.encoded();

            Class rawParameterType = Types.getRawType(parameterType);
            if (Iterable.class.isAssignableFrom(rawParameterType)) {
              if (!(parameterType instanceof ParameterizedType)) {
                throw parameterError(i, rawParameterType.getSimpleName()
                    + " must include generic type (e.g., "
                    + rawParameterType.getSimpleName()
                    + ")");
              }
              ParameterizedType parameterizedType = (ParameterizedType) parameterType;
              Type iterableType = Utils.getParameterUpperBound(0, parameterizedType);
              Converter converter =
                  retrofit.stringConverter(iterableType, parameterAnnotations);
              action = new RequestAction.Query<>(name, converter, encoded).iterable();
            } else if (rawParameterType.isArray()) {
              Class arrayComponentType = boxIfPrimitive(rawParameterType.getComponentType());
              Converter converter =
                  retrofit.stringConverter(arrayComponentType, parameterAnnotations);
              action = new RequestAction.Query<>(name, converter, encoded).array();
            } else {
              Converter converter =
                  retrofit.stringConverter(parameterType, parameterAnnotations);
              action = new RequestAction.Query<>(name, converter, encoded);
            }

            gotQuery = true;

          } 
          .......//代码省略n多行

RequestFactory基本上搞完了,原来就是因为它才让我们写请求如此的简单吧,再次感谢JW,上次还和他私信了,哈哈。。。。

3.现在就看MethodHandler的第三个参数:CallAdapter,这个比较恶心吧,个人感觉:

 //MethodHanlder#create方法中:
 CallAdapter callAdapter = createCallAdapter(method, retrofit);

 //createCallAdapter方法:
 private static CallAdapter createCallAdapter(Method method, Retrofit retrofit) {
    Type returnType = method.getGenericReturnType();
    if (Utils.hasUnresolvableType(returnType)) {
      throw Utils.methodError(method,
          "Method return type must not include a type variable or wildcard: %s", returnType);
    }
    if (returnType == void.class) {
      throw Utils.methodError(method, "Service methods cannot return void.");
    }
    Annotation[] annotations = method.getAnnotations();
    try {
      return retrofit.callAdapter(returnType, annotations);
    } catch (RuntimeException e) { // Wide exception range because factories are user code.
      throw Utils.methodError(e, method, "Unable to create call adapter for %s", returnType);
    }
  }

在后来,我们又发现了还是请求了retrofit的callAdapter,进吧:

public CallAdapter nextCallAdapter(CallAdapter.Factory skipPast, Type returnType,
      Annotation[] annotations) {
    checkNotNull(returnType, "returnType == null");
    checkNotNull(annotations, "annotations == null");

    int start = adapterFactories.indexOf(skipPast) + 1;
    **for (int i = start, count = adapterFactories.size(); i < count; i++) {
      CallAdapter adapter = adapterFactories.get(i).get(returnType, annotations, this);
      if (adapter != null) {
        return adapter;
      }
    }**
   .....//此处省略n行

看加粗的地方,需要在adapterFactories里面找啊,好吧,找遍了Retrofit类,也没有发现adapterFactories里面被赋值了,突然在Retrofit#Builder中发现:

 public Builder addCallAdapterFactory(CallAdapter.Factory factory) {
      adapterFactories.add(checkNotNull(factory, "factory == null"));
      return this;
    }

//Retrofit#builder#build方法中
//adapterFactories会优先添加用户添加的CallAdapter,最后会默认添加一个与平台有关的CallAdapter
List adapterFactories = new ArrayList<>(this.adapterFactories);
adapterFactories.add(Platform.get().defaultCallAdapterFactory(callbackExecutor));

这个addCallAdapterFactory是不是哪里用过呢,好吧,我原谅你的记性,看看我们的RetrofitUtils:

retrofit = builder.baseUrl(Constant.BASE_IP)
                        .client(okHttpClient)
                        .addConverterFactory(gsonConverterFactory)
                        .**addCallAdapterFactory(RxJavaCallAdapterFactory.create())**
                        .build();

现在这个RxJavaCallAdapterFactory.create()是什么鬼,这个Adapter相信很多人都知道其作用,就是把相关结果封装成我们喜欢操作的方式,进去看看啊:

public final class RxJavaCallAdapterFactory extends CallAdapter.Factory {
  /**
   * TODO
   */
  public static RxJavaCallAdapterFactory create() {
    return new RxJavaCallAdapterFactory();
  }

  private RxJavaCallAdapterFactory() {
  }

@Override
  public CallAdapter get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    Class rawType = getRawType(returnType);
    boolean isSingle = "rx.Single".equals(rawType.getCanonicalName());
    **if (rawType != Observable.class && !isSingle) {
      return null;
    }**
    if (!(returnType instanceof ParameterizedType)) {
      String name = isSingle ? "Single" : "Observable";
      throw new IllegalStateException(name + " return type must be parameterized"
          + " as " + name + " or " + name + "");
    }

    CallAdapter> callAdapter = getCallAdapter(returnType);
    if (isSingle) {
      // Add Single-converter wrapper from a separate class. This defers classloading such that
      // regular Observable operation can be leveraged without relying on this unstable RxJava API.
      return SingleHelper.makeSingle(callAdapter);
    }
    return callAdapter;
  }

  private CallAdapter> getCallAdapter(Type returnType) {
    Type observableType = getParameterUpperBound(0, (ParameterizedType) returnType);
    Class rawObservableType = getRawType(observableType);
    if (rawObservableType == Response.class) {
      if (!(observableType instanceof ParameterizedType)) {
        throw new IllegalStateException("Response must be parameterized"
            + " as Response or Response");
      }
      Type responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
      return new ResponseCallAdapter(responseType);
    }

    if (rawObservableType == Result.class) {
      if (!(observableType instanceof ParameterizedType)) {
        throw new IllegalStateException("Result must be parameterized"
            + " as Result or Result");
      }
      Type responseType = getParameterUpperBound(0, (ParameterizedType) observableType);
      return new ResultCallAdapter(responseType);
    }

    return new **SimpleCallAdapter**(observableType);
  }

static final class **SimpleCallAdapter** implements CallAdapter> {
    private final Type responseType;

    SimpleCallAdapter(Type responseType) {
      this.responseType = responseType;
    }

    @Override public Type responseType() {
      return responseType;
    }

    @Override public  Observable adapt(Call call) {
      return Observable.create(new CallOnSubscribe<>(call)) //
          .flatMap(new Func1, Observable>() {
            @Override public Observable call(Response response) {
              if (response.isSuccess()) {
                return Observable.just(response.body());
              }
              return Observable.error(new HttpException(response));
            }
          });
    }
  }

这里又涉及到了RxJava里面,也不扯了,不是特别熟悉,但是能使用的水平就不丢人现眼了,主要是看public CallAdapter

adapterFactories.add(Platform.get().defaultCallAdapterFactory(callbackExecutor));

现在来看Platform类,又是一个十分恶心的类:

class Platform {
  private static final Platform PLATFORM = findPlatform();

  static Platform get() {
    return PLATFORM;
  }

  //根据各个平台的核心类来获取当前的平台
  private static Platform findPlatform() {
    try {
      //获取当前Android
      Class.forName("android.os.Build");
      if (Build.VERSION.SDK_INT != 0) {
        return new Android();
      }
    } catch (ClassNotFoundException ignored) {
    }
    try {
      //获取java大法
      Class.forName("java.util.Optional");
      return new Java8();
    } catch (ClassNotFoundException ignored) {
    }
    try {
     //org.robovm.apple.foundation.NSObject
     // NSObject基类foundation框架 这个是关于apple的平台 打住不讨论了
      Class.forName("org.robovm.apple.foundation.NSObject");
      return new IOS();
    } catch (ClassNotFoundException ignored) {
    }
    return new Platform();
  }

显然我们是Andorid了,来看Android了:

static class Android extends Platform {
    @Override CallAdapter.Factory defaultCallAdapterFactory(Executor callbackExecutor) {
      if (callbackExecutor == null) {
        callbackExecutor = new MainThreadExecutor();
      }
      return new ExecutorCallAdapterFactory(callbackExecutor);
    }

    //终于看到了Android中熟悉的Handler,显然本Handler是UI线程中跟新UI所使用的,因此可以猜测
    //MainThreadExecutor是用于请求之后,用于UI数据的
    static class MainThreadExecutor implements Executor {
      private final Handler handler = new Handler(Looper.getMainLooper());

      @Override public void execute(Runnable r) {
        handler.post(r);
      }
    }
  }

4.MethodHandler中最后一个属性 Converter,当然这就是一种转换方式吧,将网络请求结果值转成我们想要的格式,我们比较熟悉的就是JSON/XML了吧,也是在Retrofit#Builder控制的,这里只需要到用的时候再说吧。

 public Builder addConverterFactory(Converter.Factory factory) {
      converterFactories.add(checkNotNull(factory, "factory == null"));
      return this;
    }

好了,MethodHandler处理完了,再次回到Retorfit.create方法中:

public  T create(final Class service) {
     return loadMethodHandler(method).invoke(args);
}

现在来看MethodHandler.invoke方法了:

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

在Retrofit中:

//Retrofit#nextCallAdapter
 for (int i = start, count = adapterFactories.size(); i < count; i++) {
      CallAdapter adapter = adapterFactories.get(i).get(returnType, annotations, this);
      if (adapter != null) {
        return adapter;
      }
    }

RxJavaAdapterFactory返回的null[具体原因可以看get值],那么此时adapter就应该为new ExecutorCallAdapterFactory(MainThreadExecutor executor)了,原因看Platform平台中Android,这里就不说了,上面也写了有的。现在来看ExecutorCallAdapterFactory源码了:

final class ExecutorCallAdapterFactory extends CallAdapter.Factory {
  final Executor callbackExecutor;

  ExecutorCallAdapterFactory(Executor callbackExecutor) {
    this.callbackExecutor = callbackExecutor;
  }

  //此时我们得到了get方法
  @Override
  public CallAdapter> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    if (getRawType(returnType) != Call.class) {
      return null;
    }
    final Type responseType = Utils.getCallResponseType(returnType);
    return new CallAdapter>() {
      @Override public Type responseType() {
        return responseType;
      }

      @Override public  Call adapt(Call call) {
        //调用了新的ExecutorCallbackCall类,
        //此时callbackExecutor是MainThreadExecutor
        //call是我们前面的OkHttpCall,原始是MethodHandler的invoke中一句话:
        //***return callAdapter.adapt(
        //new OkHttpCall<>(callFactory, requestFactory, args, responseConverter)); **

        return new ExecutorCallbackCall<>(callbackExecutor, call);
      }
    };
  }

  static final class ExecutorCallbackCall<T> implements Call<T> {
    final Executor callbackExecutor;
    final Call delegate;

    ExecutorCallbackCall(Executor callbackExecutor, Call delegate) {
      this.callbackExecutor = callbackExecutor;
      this.delegate = delegate;
    }

    //这里delegate就是OkHttpCall,delegate的原意是代理的意思,enqueue方法也就是代理模式的一种写法
    @Override public void enqueue(final Callback callback) {
      //这里将直接由delegate进行异步请求了,当返回结果时,callbackExecutor将作在UI线程中进行结果回调
      delegate.enqueue(new Callback() {
        @Override public void onResponse(final Call call, final Response response) {
          callbackExecutor.execute(new Runnable() {
            @Override public void run() {
              if (delegate.isCanceled()) {
                // Emulate OkHttp's behavior of throwing/delivering an IOException on cancelation
                callback.onFailure(call, new IOException("Canceled"));
              } else {
                callback.onResponse(call, response);
              }
            }
          });
        }

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

现在我们来看OkHttpCall#enqueue方法:

@Override public void enqueue(final Callback callback) {
    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 {
          //创建Call对象
          call = rawCall = createRawCall();
        } catch (Throwable t) {
          failure = creationFailure = t;
        }
      }
    }

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

    if (canceled) {
      call.cancel();
    }

    //这应该是**正式**的请求了,此时call将直接转移到了OKHttp中请求,具体的请求方式我感觉有时间我会去研究的
    call.enqueue(new okhttp3.Callback() {
      @Override public void onResponse(okhttp3.Call call, okhttp3.Response rawResponse)
          throws IOException {
        Response response;
        try {
          response = parseResponse(rawResponse);
        } catch (Throwable e) {
          callFailure(e);
          return;
        }
        callSuccess(response);
      }

      @Override public void onFailure(okhttp3.Call call, IOException e) {
        try {
          callback.onFailure(OkHttpCall.this, e);
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }

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

      private void callSuccess(Response response) {
        try {
          callback.onResponse(OkHttpCall.this, response);
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }
    });
  }

当然 我么来看这个call获取方法createRawCall:

private okhttp3.Call createRawCall() throws IOException {
    okhttp3.Call call = **callFactory.newCall(requestFactory.create(args))**;
    if (call == null) {
      throw new NullPointerException("Call.Factory returned null.");
    }
    return call;
  }

此时我们知道了,MethodHandler中的第二个参数RequestFactory终于发挥作用了,在这里组装并通过OkHttp3进行网络请求了,requestFactory.create(args)也使用了builder模式:

//RequestFactory#create方法
Request create(Object... args) throws IOException {
    RequestBuilder requestBuilder =
        new RequestBuilder(method, baseUrl.url(), relativeUrl, headers, contentType, hasBody,
            isFormEncoded, isMultipart);

    if (args != null) {
      RequestAction[] actions = requestActions;
      if (actions.length != args.length) {
        throw new IllegalArgumentException("Argument count ("
            + args.length
            + ") doesn't match action count ("
            + actions.length
            + ")");
      }
      for (int i = 0, count = args.length; i < count; i++) {
        actions[i].perform(requestBuilder, args[i]);
      }
    }

    return requestBuilder.build();
  }

当然还有parseResponse(rawResponse)方法,这就是我们将获取的ResponseBody转换成我们想要的格式:

Response 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();

    int code = rawResponse.code();
    //当初就是因为没有看这里的源码,就写了issues给Jake Wharton 没过10mins 给我这个菜鸟回复了
    //非常感动 立志要学好android啊....
    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) {
      return Response.success(null, rawResponse);
    }

    ExceptionCatchingRequestBody catchingBody = new ExceptionCatchingRequestBody(rawBody);
    try {
      **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;
    }
  }

所有的方法也就基本上分析了一遍,还有很多地方没有仔细看,不过现在最主要的就是

//MethodHandler#
Object invoke(Object... args) {
    return callAdapter.adapt(
        new OkHttpCall<>(callFactory, requestFactory, args, responseConverter));
  }

在adapt方法中:

@Override public  Call adapt(Call call) {
        return new ExecutorCallbackCall<>(callbackExecutor, call);
      }

在ExecutorCallbackCall中存在delegate.enqueue方法,此方法就是执行网络请求的过程。
那么我们触发了Retrofit的请求方式 就是call.enqueue/execute 了。。。。

写了很长时间,中间也想放弃了,也卡住好几次,但是慢慢坚持一下吧,菜鸟刚开始写源码分析,写得不好的地方大家见谅,有什么问题也可以提出来,大家共同进步。啦啦啦啦啦,

你可能感兴趣的:(android进阶)