Retrofit-12 Analysis of network request interface instance in retrofit

public interface MyInterface {
  @GET(“…/…”)
  Call<List<MyResponse>> getCall();
}
The front part of ".../..." is the baseUrl address, and the latter part is the address of the annotation.

Note: If we have set a complete URL address in the parameter after the annotation, then we can not set the BaseUrl parameter when setting the Retrofit object.


Here is the code in the Retrofit.java class:

// Single-interface proxy creation guarded by parameter safety.
@SuppressWarnings("unchecked")
  public <T> T create(final Class<T> service) {
    // Verify the incoming interface bytecode
    Utils.validateServiceInterface(service);
    // Flag bit, indicating whether this interface needs to be verified and parsed in advance
    if (validateEagerly) {
      eagerlyValidateMethods(service);
}
// use the dynamic proxy to operate
    return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
        new InvocationHandler() {
          private final Platform platform = Platform.get();
          @Override // Where the interface operation is really resolved
          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);
            }
            // Important three lines of code
            ServiceMethod<Object, Object> serviceMethod =
                (ServiceMethod <Object, Object>) loadServiceMethod (method);
OkHttpCall<Object> okHttpCall =
             new OkHttpCall<>(serviceMethod, args);
            return serviceMethod.adapt(okHttpCall);
          }
        });
  }

boolean isDefaultMethod(Method method) {
   return false;
}

ServiceMethod : The method corresponding to an interface, which encapsulates it. In this, you will see that there are many network configuration request parameters, including URL and so on.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324469491&siteId=291194637