4.retrofit

本文深入剖析Retrofit框架的工作原理,从动态代理创建接口代理对象开始,详细解读如何使用注解声明请求,创建Retrofit对象,以及如何通过Retrofit进行数据请求。文章还探讨了Retrofit如何将请求信息转换为OkHttp请求,以及如何处理请求结果。
摘要由CSDN通过智能技术生成

简单使用

  1. 新建接口,并用注解声明请求

    public interface GitHub {
     @GET("/repos/{owner}/{repo}/contributors")
     Call<List<Contributor>> contributors(
         @Path("owner") String owner,
         @Path("repo") String repo);
      }
    
  2. 创建Retrofit对象

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(API_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    
  3. 用retrofit对象创建GitHub接口实现类,并使用

    GitHub github = retrofit.create(GitHub.class);
    Call<List<Contributor>> call = github.contributors("square", "retrofit");
    call.enqueue(new Callback<List<Contributor>>() {
           @Override
           public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) {
               List<Contributor> contributors = response.body();
               for (Contributor contributor : contributors) {
                   System.out.println(contributor.login + " (" + contributor.contributions + ")");
               }
           }
           @Override
        public void onFailure(Call<List<Contributor>> call, Throwable t) {
           }
    });
    

源码分析

看源码首先要找到切入点,而这个切入点应该是框架直接与我们应用进行交互的入口,在上面就是call.enqueue(),但是当我们点击enqueue方法进去看到是如下内容

/**
 * Asynchronously send the request and notify {@code callback} of its response or if an error
 * occurred talking to the server, creating the request, or processing the response.
 */
void enqueue(Callback<T> callback);

我们只能看到一个抽象方法,这些不能让我们知道他的实现,这时我们要回退一步去查找call对象是怎么创建的,但这是我们又发现这个Call就是我们在声明的方法的返回对象,我们需要再次回退一步,这是就需要我们看retrofit.create(GitHub.class)这行代码,我们需要查看create方法的源码

create方法

create方法的源码如下

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 {
                    // 如果该方法是Object的方法,则执行常规调用。
                    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);
                }
            });
}

可以看到,除了对接口的校验外,这个方法主要的代码就是Proxy.newProxyInstance的方法了,看到这里我们可以知道了,调用create方法创建接口实现类是通用Java的动态代理实现的

动态代理

利用反射机制在运行时创建代理类,当调用代理类方法是,会执行 创建代理类 时传入的InvocationHandler对象的invoke方法,在这个方法里我们可以对此时调用的方法进行处理。

详情请看https://www.jianshu.com/p/9bcac608c714

retrofit就是用动态代理,创建了我们需要接口的代理类,使得我们不需要自己实现接口的功能,就可以使用我们定义的接口。

此时我们有了GitHub类的代理对象,当我们用这个代理对象调用我们定义的接口时,会调用InvocationHandler对象的invoke方法,我们回到源码。可以看到前几行是对方法做判断防止调用的不是我们用于声明请求的方法。主管的关键是最下面的三行代码

在这里插入图片描述

直接看这三行代码我们是无法看懂它在做什么,我们只能进入代码去看下,各行代码是做什么的。

首先我们看loadServiceMethod方法的代码,如下

 /**
     * 加载当前方法的ServiceMethod
     *
     * @param method 当前调用方法
     * @return ServiceMethod
     */
    ServiceMethod<?, ?> loadServiceMethod(Method method) {
        //从缓存获取
        ServiceMethod<?, ?> result = serviceMethodCache.get(method);
        if (result != null) return result;

        synchronized (serviceMethodCache) {
            //加锁,防止其他线程创建该对象的ServiceMethod,保证其唯一
            result = serviceMethodCache.get(method);
            if (result == null) {
                result = new ServiceMethod.Builder<>(this, method).build();
                serviceMethodCache.put(method, result);
            }
        }
        return result;
    }

这些代码主要是要获得ServiceMethod的对象,ServiceMethod类的注释是Adapts an invocation of an interface method into an HTTP call.(将接口方法的调用调整为HTTP调用。)它的主要作用是将我们接口声明的请求信息组装为OkHttp的请求对象Request。和将OkHttp请求的ResponseBody转化为我们需要的返回类型T.

首先是从缓存中查,没有的话创建一个并存到缓存中

接下来我们看他的创建代码,这里用到了建造这模式,首先看ServiceMethod.Builder的构造方法

Builder(Retrofit retrofit, Method method) {
    this.retrofit = retrofit;
    this.method = method;
    //获取方法的注解
    this.methodAnnotations = method.getAnnotations();
    //获取参数类型数组
    this.parameterTypes = method.getGenericParameterTypes();
    //获取方法的参数类型对应的注解
    this.parameterAnnotationsArray = method.getParameterAnnotations();
}

再接下来看build()方法

public ServiceMethod build() {
    callAdapter = createCallAdapter();
    responseType = callAdapter.responseType();
    if (responseType == Response.class || responseType == okhttp3.Response.class) {
        throw methodError("'"
                + Utils.getRawType(responseType).getName()
                + "' is not a valid response body type. Did you mean ResponseBody?");
    }
    //响应转换器,将相应对象转换为我们需要的对象类型
    responseConverter = createResponseConverter();
    for (Annotation annotation : methodAnnotations) {
        //解析方法注解,如@GET等
        parseMethodAnnotation(annotation);
    }
    //***********省略一些异常判断代理************
    int parameterCount = parameterAnnotationsArray.length;
    parameterHandlers = new ParameterHandler<?>[parameterCount];
    for (int p = 0; p < parameterCount; p++) {
        Type parameterType = parameterTypes[p];
        if (Utils.hasUnresolvableType(parameterType)) {
            throw parameterError(p, "Parameter type must not include a type variable or wildcard: %s",
                    parameterType);
        }
        Annotation[] parameterAnnotations = parameterAnnotationsArray[p];
        if (parameterAnnotations == null) {
            throw parameterError(p, "No Retrofit annotation found.");
        }
        parameterHandlers[p] = parseParameter(p, parameterType, parameterAnnotations);
    }
    //****************这里省去一些参数校验的代码******************
    return new ServiceMethod<>(this);
}

首先是创建一个CallAdapterCallAdapter是retrofit里的定义的调用(Call)适配器,并提供将retrofit的调用请求对象Call<R>转换为第三的请求对象,如RxJava的Single<R>。

createCallAdapter源码解析
/**
 * 创建CallAdapter
 *
 * @return CallAdapter,
 */
private CallAdapter<T, R> createCallAdapter() {
    Type returnType = method.getGenericReturnType();
    if (Utils.hasUnresolvableType(returnType)) {
        //方法返回类型不得包含类型变量或通配符
        throw methodError(
                "Method return type must not include a type variable or wildcard: %s", returnType);
    }
    if (returnType == void.class) {
        //服务方法不能返回void
        throw methodError("Service methods cannot return void.");
    }
    Annotation[] annotations = method.getAnnotations();
    try {
        //noinspection unchecked根据返回类型返回一个合适的CallAdapter
        return (CallAdapter<T, R>) retrofit.callAdapter(returnType, annotations);
    } catch (RuntimeException e) { // Wide exception range because factories are user code.
        throw methodError(e, "Unable to create call adapter for %s", returnType);
    }
}

可以看到是调用了retrofit.callAdapter,点击进入继续分析

public CallAdapter<?, ?> callAdapter(Type returnType, Annotation[] annotations) {
    return nextCallAdapter(null, returnType, annotations);
}

继续进入nextCallAdapter

/**
 * 从{@code skipPast}之外的可用{@linkplain #callAdapterFactories() 工厂}返回{@code returnType}的{@link CallAdapter}。
 *
 * @throws IllegalArgumentException if no call adapter available for {@code type}.
 */
public CallAdapter<?, ?> nextCallAdapter(@Nullable 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;
        }
    }
   //***********省略一些出错信息整合代码
    throw new IllegalArgumentException(builder.toString());
}

这里是通过for循环不断的从adapterFactories里查找适合当前返回类型和注解的CallAdapter对象

adapterFactories是在RetrofitBuilder.build()的时候初始化的

List<CallAdapter.Factory> adapterFactories = new ArrayList<>(this.adapterFactories);
adapterFactories.add(platform.defaultCallAdapterFactory(callbackExecutor));

platform.defaultCallAdapterFactory获取到的factory是ExecutorCallAdapterFactory,这个factory可以生成转换ExecutorCallbackCallCallAdapter,而这就是默认的Call<R>的CallAdapter

我们也可以添加其他的CallAdapter.Factory,例如RxJava2CallAdapterFactory这样就支持Single<T>这种RxJava的请求类型了

接下来是获得方法的实际返回类型responseType,也就是Call<R>里的R

然后创建一个响应转换器responseConverter用于将OkHttp的ResponseBody对象转换为上面的responseType

createResponseConverter源码分析
private Converter<ResponseBody, T> createResponseConverter() {
    Annotation[] annotations = method.getAnnotations();
    try {
        return retrofit.responseBodyConverter(responseType, annotations);
    } catch (RuntimeException e) { // Wide exception range because factories are user code.
        throw methodError(e, "Unable to create converter for %s", responseType);
    }
}

retrofit.responseBodyConverter方法

public <T> Converter<ResponseBody, T> responseBodyConverter(Type type, Annotation[] annotations) {
    return nextResponseBodyConverter(null, type, annotations);
}

nextResponseBodyConverter方法

public <T> Converter<ResponseBody, T> nextResponseBodyConverter(
        @Nullable Converter.Factory skipPast, Type type, Annotation[] annotations) {
     //*********省略判空检查源码
    int start = converterFactories.indexOf(skipPast) + 1;
    for (int i = start, count = converterFactories.size(); i < count; i++) {
        Converter<ResponseBody, ?> converter =
                converterFactories.get(i).responseBodyConverter(type, annotations, this);
        if (converter != null) {
            //noinspection unchecked
            return (Converter<ResponseBody, T>) converter;
        }
    }
  //*********异常信息组合源码
    throw new IllegalArgumentException(builder.toString());
}

可以看到同获取CallAdapter一样

converterFactoriesnew Retrofit.Builder()时初始化加入默认Factory

Builder(Platform platform) {
    this.platform = platform;
    converterFactories.add(new BuiltInConverters());
}

BuiltInConverters这个Converter只能解析ResponseBody类型的返回数据也就是说默认只支持Call<ResponseBody>的Call

所以一般我们会加入GsonConverterFactory等转换器

然后解析方法注解,将GET POST FormUrlEncoded 等请求注解的相关信息获取到并记录下来。

parseMethodAnnotation源码分析
private void parseMethodAnnotation(Annotation annotation) {
    if (annotation instanceof DELETE) {
        parseHttpMethodAndPath("DELETE", ((DELETE) annotation).value(), false);
    } else if (annotation instanceof GET) {
        parseHttpMethodAndPath("GET", ((GET) annotation).value(), false);
    } else if (annotation instanceof HEAD) {
        parseHttpMethodAndPath("HEAD", ((HEAD) annotation).value(), false);
        if (!Void.class.equals(responseType)) {
            throw methodError("HEAD method must use Void as response type.");
        }
    } else if (annotation instanceof PATCH) {
        parseHttpMethodAndPath("PATCH", ((PATCH) annotation).value(), true);
    } else if (annotation instanceof POST) {
        parseHttpMethodAndPath("POST", ((POST) annotation).value(), true);
    } else if (annotation instanceof PUT) {
        parseHttpMethodAndPath("PUT", ((PUT) annotation).value(), true);
    } else if (annotation instanceof OPTIONS) {
        parseHttpMethodAndPath("OPTIONS", ((OPTIONS) annotation).value(), false);
    } else if (annotation instanceof HTTP) {
        HTTP http = (HTTP) annotation;
        parseHttpMethodAndPath(http.method(), http.path(), http.hasBody());
    } else if (annotation instanceof retrofit2.http.Headers) {
        String[] headersToParse = ((retrofit2.http.Headers) annotation).value();
        if (headersToParse.length == 0) {
            throw methodError("@Headers annotation is empty.");
        }
        headers = parseHeaders(headersToParse);
    } else if (annotation instanceof Multipart) {
        if (isFormEncoded) {
            throw methodError("Only one encoding annotation is allowed.");
        }
        isMultipart = true;
    } else if (annotation instanceof FormUrlEncoded) {
        if (isMultipart) {
            throw methodError("Only one encoding annotation is allowed.");
        }
        isFormEncoded = true;
    }
}

这里根据不同的注解使用不同的方法解析,如GET使用parseHttpMethodAndPath方法解析

private void parseHttpMethodAndPath(String httpMethod, String value, boolean hasBody) {
    if (this.httpMethod != null) {
        throw methodError("Only one HTTP method is allowed. Found: %s and %s.",
                this.httpMethod, httpMethod);
    }
    this.httpMethod = httpMethod;
    this.hasBody = hasBody;
    if (value.isEmpty()) {
        return;
    }
    // 获取相对URL路径和现有查询字符串(如果存在)
    int question = value.indexOf('?');
    if (question != -1 && question < value.length() - 1) {
        // 确保查询字符串没有任何参数名。
        String queryParams = value.substring(question + 1);
        Matcher queryParamMatcher = PARAM_URL_REGEX.matcher(queryParams);
        if (queryParamMatcher.find()) {
            throw methodError("URL query string \"%s\" must not have replace block. "
                    + "For dynamic query parameters use @Query.", queryParams);
        }
    }
    this.relativeUrl = value;
    this.relativeUrlParamNames = parsePathParameters(value);
}

parsePathParameters方法源码

/**
 * 主要是查找路径的参数如"api/user/{path}/add"
 */
static Set<String> parsePathParameters(String path) {
    Matcher m = PARAM_URL_REGEX.matcher(path);
    Set<String> patterns = new LinkedHashSet<>();
    while (m.find()) {
        patterns.add(m.group(1));
    }
    return patterns;
}

其他的注解解析类似,就不再做分析

然后创建一个ParameterHandler数组,ParameterHandler用于参数处理,会在OkHttp的Request构建是用于将各个参数对象处理到请求中。

然后就是将一个个 参数 以及 参数的注解 解析为ParameterHandler对象。

parseParameter方法源码解析
private ParameterHandler<?> parseParameter(
     int p, Type parameterType, Annotation[] annotations) {
 ParameterHandler<?> result = null;
 for (Annotation annotation : annotations) {
     ParameterHandler<?> annotationAction = parseParameterAnnotation(
             p, parameterType, annotations, annotation);
     if (annotationAction == null) {
         continue;
     }
     if (result != null) {
         throw parameterError(p, "Multiple Retrofit annotations found, only one all
     }
     result = annotationAction;
 }
 if (result == null) {
     throw parameterError(p, "No Retrofit annotation found.");
 }
 return result;

parseParameterAnnotation方法源码很长,也是一个注解一种处理方案,这里摘取因部分代码

private ParameterHandler<?> parseParameterAnnotation( int p, Type type, Annotation[] annotations, Annotation annotation) {
  if (annotation instanceof Query) {
	Query query = (Query) annotation;
	String name = query.value();
	boolean encoded = query.encoded();
	Class<?> rawParameterType = Utils.getRawType(type);
	gotQuery = true;
	if (Iterable.class.isAssignableFrom(rawParameterType)) {
 	//实际类型是参数化类型的话抛出异常
	 ParameterizedType parameterizedType = (ParameterizedType) type;
 	Type iterableType = Utils.getParameterUpperBound(0, parameterizedType);
	    Converter<?, String> converter =
         retrofit.stringConverter(iterableType, annotations);
	    return new ParameterHandler.Query<>(name, converter, encoded).iterable();
	} else if (rawParameterType.isArray()) {
	    Class<?> arrayComponentType = boxIfPrimitive(rawParameterType.getComponentType());
	    Converter<?, String> converter =retrofit.stringConverter(arrayComponentType, annotations);
	    return new ParameterHandler.Query<>(name, converter, encoded).array();
	} else {
	    Converter<?, String> converter =
         retrofit.stringConverter(type, annotations);
	    return new ParameterHandler.Query<>(name, converter, encoded);
	}
}

ParameterHandler.Query源码

static final class Query<T> extends ParameterHandler<T> {
    private final String name;
    private final Converter<T, String> valueConverter;
    private final boolean encoded;

    Query(String name, Converter<T, String> valueConverter, boolean encoded) {
      this.name = checkNotNull(name, "name == null");
      this.valueConverter = valueConverter;
      this.encoded = encoded;
  }

    /**构建Request时会调用此方法
     * @param builder request的构造器
     * @param value 参数值
     */
    @Override void apply(RequestBuilder builder, @Nullable T value) throws IOException {
      if (value == null) return; // Skip null values.

      String queryValue = valueConverter.convert(value);
      if (queryValue == null) return; // Skip converted but null values

      builder.addQueryParam(name, queryValue, encoded);
    }
  }

然后用这个Builder对象创建ServiceMethod对象,

这样ServiceMethod对象创建完成

此时继续看InvocationHandler.invoke方法里的源码

OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);

用上面获取到的serviceMethod和实际请求参数构造了一个OkHttpCall对象

而我们看OkHttpCall方法源码

OkHttpCall(ServiceMethod<T, ?> serviceMethod, @Nullable Object[] args) {
    this.serviceMethod = serviceMethod;
    this.args = args;
}

只是进行了简单的赋值,明显重点在要看后面了,

我们继续看InvocationHandler.invoke方法的最后一行代码

return serviceMethod.callAdapter.adapt(okHttpCall);

调用了serviceMethod.callAdapteradapt(okHttpCall)方法,而根据上面的分析默认情况下serviceMethod.callAdapterExecutorCallAdapterFactory下的匿名内部类CallAdapter

而我们看CallAdapteradapt方法

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

也就是说我们调用完github.contributors("square", "retrofit")后获取到的是一个ExecutorCallbackCall对象

这是我们就找到了我们的call对象的类型,自然而然的我们去看ExecutorCallbackCallenqueue方法

@Override public void enqueue(final Callback<T> callback) {
  checkNotNull(callback, "callback == null");
  delegate.enqueue(new Callback<T>() {
    @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);
          }
        }
      });
    }
    @Override public void onFailure(Call<T> call, final Throwable t) {
      callbackExecutor.execute(new Runnable() {
        @Override public void run() {
          callback.onFailure(ExecutorCallbackCall.this, t);
        }
      });
    }
  });

关键是delegate.enqueue的调用,而delegate是在构造函数里传入的OkHttpCall<Object>

ExecutorCallbackCall的构造方法

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

我们进入OkHttpCall.enqueue方法

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 {
                //创建RawCall
                call = rawCall = createRawCall();
            } catch (Throwable t) {
                failure = creationFailure = t;
            }
        }
    }
    if (failure != null) {
        callback.onFailure(this, failure);
        return;
    }
    if (canceled) {
        call.cancel();
    }
    //okHttp的异步请求
    call.enqueue(new okhttp3.Callback() {
        @Override
        public void onResponse(okhttp3.Call call, okhttp3.Response rawResponse)
                throws IOException {
            Response<T> response;
            try {
                //对状态码判断,并将okhttp3.Response转换为retrofit的Response<T>
                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<T> response) {
            try {
                callback.onResponse(OkHttpCall.this, response);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    });
}

可以看到先是创建了okhttp3.Call,然后调用okhttp3.Call的异步请求方法完成请求

我们再去看下如何完成的okhttp3.Call的创建-createRawCall()方法

private okhttp3.Call createRawCall() throws IOException {
    Request request = serviceMethod.toRequest(args);
    //生成okhttp3.Call
    okhttp3.Call call = serviceMethod.callFactory.newCall(request);
    if (call == null) {
        throw new NullPointerException("Call.Factory returned null.");
    }
    return call;
}

可以看到首先是通过serviceMethod生成了OkHttp的Request对象,这样就要我们上面生成serviceMethod联系起来了。

我们再看下serviceMethod.toRequest(args)方法

/**
 * Builds an HTTP request from method arguments.
 * 根据方法参数构建HTTP请求
 */
Request toRequest(@Nullable Object... args) throws IOException {
    RequestBuilder requestBuilder = new RequestBuilder(httpMethod, baseUrl, relativeUrl, headers,
            contentType, hasBody, isFormEncoded, isMultipart);
    @SuppressWarnings("unchecked") // It is an error to invoke a method with the wrong arg types.
            ParameterHandler<Object>[] handlers = (ParameterHandler<Object>[]) parameterHandlers;
    int argumentCount = args != null ? args.length : 0;
    if (argumentCount != handlers.length) {
        throw new IllegalArgumentException("Argument count (" + argumentCount
                + ") doesn't match expected count (" + handlers.length + ")");
    }
    for (int p = 0; p < argumentCount; p++) {
        //参数和起注解解析出的ParameterHandler使用
        handlers[p].apply(requestBuilder, args[p]);
    }
    //创建一个OkHttp的Request
    return requestBuilder.build();
}

RequestBuilder是retrofit 为了创建OkHttp的Request而打造的构建器,我们看下requestBuilder.build()的源码

Request build() {
  HttpUrl url;
  HttpUrl.Builder urlBuilder = this.urlBuilder;
  if (urlBuilder != null) {
    url = urlBuilder.build();
  } else {
    // No query parameters triggered builder creation, just combine the relative URL and base URL.
    // 没有查询参数会触发构建器创建,只需结合相对URL和基本URL
    //noinspection ConstantConditions Non-null if urlBuilder is null.
    url = baseUrl.resolve(relativeUrl);
    if (url == null) {
      throw new IllegalArgumentException(
          "Malformed URL. Base: " + baseUrl + ", Relative: " + relativeUrl);
    }
  }
  RequestBody body = this.body;
  if (body == null) {
    // Try to pull from one of the builders.
    if (formBuilder != null) {
      body = formBuilder.build();
    } else if (multipartBuilder != null) {
      body = multipartBuilder.build();
    } else if (hasBody) {
      // Body is absent, make an empty body.Body不存在,使Body为空。
      body = RequestBody.create(null, new byte[0]);
    }
  }
  MediaType contentType = this.contentType;
  if (contentType != null) {
    if (body != null) {
      body = new ContentTypeOverridingRequestBody(body, contentType);
    } else {
      requestBuilder.addHeader("Content-Type", contentType.toString());
    }
  }
  return requestBuilder
      .url(url)
      .method(method, body)
      .build();
}

可以看到在这里是真正的使用了,之前ServiceMethod解析出的所有信息,用于生成OkHttp的Request

接下来回到okhttp3.CallcreateRawCall()方法

接下来使用上面生成request对象,生成okhttp3.Call

okhttp3.Call call = serviceMethod.callFactory.newCall(request);

上面代码已经是OkHttp的调用了,serviceMethod.callFactory默认是OkHttpClient

我们再回到OkHttpCall.enqueue方法

可以看到直接在这个方法里进行了异步请求,而且将请求结果处理好后再调用callback.onResponse(OkHttpCall.this, response)回掉请求结果或者调用callback.onFailure(OkHttpCall.this, e)回掉请求出错

这是我们不要忘记了,这个OkHttpCall.enqueue是在ExecutorCallbackCall里的enqueue方法里调用的

接下来我们回看ExecutorCallbackCall.enqueue

@Override 
public void enqueue(final Callback<T> callback) {
  checkNotNull(callback, "callback == null");
  delegate.enqueue(new Callback<T>() {
    @Override public void onResponse(Call<T> call, final Response<T> response) {
      callbackExecutor.execute(new Runnable() {
        @Override public void run() {
          if (delegate.isCanceled()) {
            
            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);
        }
      });
    }
  });
}

delegateOkHttpCall对象,而 它调用的enqueue方法传入的Callback里又有一个callbackExecutor.execute(runable)代码,而Runnable的run方法才真正的调用了,我们应用代码传过来的Callback<T>

我们需要看下callbackExecutor是什么。

最后通过重重回复我们发现callbackExecutor默认是MainThreadExecutor

MainThreadExecutor

static class MainThreadExecutor implements Executor {
  private final Handler handler = new Handler(Looper.getMainLooper());
  @Override public void execute(Runnable r) {
    handler.post(r);
  }
}

可以看到它主要是做了,切换到主线程的操作。

总结

默认情况下

  1. retrofit先是使用动态代理创建了请求接口的代理对象

  2. 当我们用代理对象调用请求方法获得请求对象(如Call<R>)时,会先获取到ServiceMethod对象,ServiceMethod对象存储有该请求方法的所有请求信息

  3. 当我们用请求对象进行请求时,会使用ServiceMethod存储的请求信息构建OkHttp的请求对象,然后再使用OkHttp的请求对象进行数据请求

  4. 请求完成后会将OkHttp的请求结果使用ServiceMethod的相关转换器转换为我们使用的对象,然后再用MainThreadExecutor切换到主线程将我们的结果回掉回来

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值