dxracer轮子怎么拆_椅子滚轮怎么拆[通俗易懂]

dxracer轮子怎么拆_椅子滚轮怎么拆[通俗易懂]一.gradle引入implementation’com.squareup.okhttp3:okhttp:3.9.1’implementation’com.squareup.retrofit2:retrofit:2.

一.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

今天的文章dxracer轮子怎么拆_椅子滚轮怎么拆[通俗易懂]分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/77867.html

(0)
编程小号编程小号

相关推荐

发表回复

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