Retrofit 拆轮子-(1)

Retrofit 拆轮子-(1)一.gradle引入implementation’com.squareup.okhttp3:okhttp:3.9.1’implementation’com.squareup.retrofit2:retrofit:2.4.0’implementation’com.squareup.retrofit2:converter-gson:2.4.0’implem…

大家好,欢迎来到IT知识分享网。

一.gradle 引入

  implementation 'com.squareup.okhttp3:okhttp:3.9.1'
    implementation 'com.squareup.retrofit2:retrofit:2.4.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0'

二.创建并使用

Retrofit retrofit = new Retrofit.Builder()
    .baseUrl("url")
     .addConverterFactory(GsonConverterFactory.create())
     ///.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
    .build();
//以上使用builder模式,外观模式

//创建一个
public interface MyApi {
  @GET("users/{user}/repos")
  Call<List> get(@Path("user") String user);
}

//获得
Api api= retrofit.create(MyApi .class);

//使用
Call<list> call = api.get("小明")
list = call.execute().boday() 

以上是retrofit 配合okhttp的简单使用,假如你不会okhttp,请点击查看

注意:
addConverterFactory(GsonConverterFactory.create()) >.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
很明显可以猜到:前者是对服务器数据进行gson解析,或者是将我们的Call 保证成Rxjava 的被观察者的方法

images

三.Retrofit 的构造函数:使用builder模式,其中

“`
public Retrofit build(){
…. 其中有许多默认的和外面穿进入的参数:
其中比较重要的是:
okhttp3.Call.Factory callFactory–> 就是OkHttpClient
List

四.retrofit#create()方法

c.create(GitHubService.class);

源码

 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 {
            //判断....
            ServiceMethod<Object, Object> serviceMethod =
                (ServiceMethod<Object, Object>) loadServiceMethod(method);
            OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
            return serviceMethod.adapt(okHttpCall);
          }
        });
  }

我进行了简化,其实就是使用动态代理的模式完成自己定义的Service 网络请求,而最关键的三句是

 ServiceMethod<Object, Object> serviceMethod =
                (ServiceMethod<Object, Object>) loadServiceMethod(method);
            OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
            return serviceMethod.adapt(okHttpCall);
4.1.ServiceMethod 的创建
  ServiceMethod(Builder<R, T> builder) {
    this.callFactory = builder.retrofit.callFactory();
    this.callAdapter = builder.callAdapter;
    this.baseUrl = builder.retrofit.baseUrl();
    this.responseConverter = builder.responseConverter;
    this.httpMethod = builder.httpMethod;
    this.relativeUrl = builder.relativeUrl;
    this.headers = builder.headers;
    this.contentType = builder.contentType;
    this.hasBody = builder.hasBody;
    this.isFormEncoded = builder.isFormEncoded;
    this.isMultipart = builder.isMultipart;
    this.parameterHandlers = builder.parameterHandlers;
  }

这里就得到了四个重要的成员:callFactory,callAdapter,parameterHandlers,callFactory

1.callFactory,负责创建Http请求,默认是okhttpclient;
2.callAdapter,最后负责把retorfit2.call变成T,这个有addCallAdapterFactory(RxJava2CallAdapterFactory.create())传人,这个是可以配合rxjava使用
3.responseConverer;解析服务器的数据:如json数据//这个可以根据需求传人不同的接下去:如xml..
4.parameterHandlerdler;

同时这个ServiMethod 提供一系列网络请求需要的方法;

以上就是retroft 的大体逻辑,可以看出这个框架的精华就是,服务器的数据的解析和最终获得的数据类型都能方便巧妙的进行;这个框架默认使用okhttp;真是优雅

images

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/9808.html

(0)

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信