目录:andorid jar/库源码解析

Retrofit2:

  作用:

    通过封装okhttp库,来进行web通讯,并且使用动态代理的方式,来调用接口地址,通过回调赋值结果。

  栗子:

  定义一个接口,用于访问使用。

public interface IServiceApi {
@FormUrlEncoded
@POST("login")
Call<LoginResult> login(@Field("name") String name, @Field("pwd") String pwd); @GET("getinfo")
Call<UserInfo> getinfo(@Query("token") String token); @GET("getinfo2")
Call<UserInfo> getinfo2(@Query("token") String token);
}

  调用接口1.可以在main中调用,因为是通过异步执行(enqueue)

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8087/").build();
IServiceApi api = retrofit.create(IServiceApi.class);
Call<LoginResult> call = api.login("test", "test1234");
call.enqueue(new Callback<LoginResult>() {
@Override
public void onResponse(Call<LoginResult> call, Response<LoginResult> response) {
LoginResult loginResult = response.body();
if(loginResult != null) {
}
} @Override
public void onFailure(Call<LoginResult> call, Throwable t) {
Log.i(tag, "ex " + t.getMessage());
}
});

  调用接口2.可以在main中调用,因为是通过异步执行(enqueue)

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8087/").build();
IServiceApi api = retrofit.create(IServiceApi.class);
Call<UserInfo> call = api.getinfo("testtesttest");
call.enqueue(new Callback<UserInfo>() {
@Override
public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
UserInfo userInfo = response.body();
if(userInfo != null) {
}
} @Override
public void onFailure(Call<UserInfo> call, Throwable t) {
Log.i(tag, "ex " + t.getMessage());
}
});

  同步调用接口3.

Retrofit retrofit = new Retrofit.Builder().addConverterFactory(GsonConverterFactory.create()).baseUrl("http://192.168.86.11:8087/").build();
IServiceApi api = retrofit.create(IServiceApi.class);
Call<LoginResult> call = api.login("test", "test1234");
try {
Response<LoginResult> resultResponse = call.execute();
LoginResult result = resultResponse.body();
}catch (Exception e){
e.printStackTrace();
}

  源码解读:

  A:异步调用

  1、创建一个Retrofit对象。

  2、retrofit.create(IServiceApi.class); // 使用Proxy.newProxyInstance 创建一个接口的代理对象。内部使用 ServiceMethod配合OkHttpCall调用,构造他们需要的对象数据

  3、调用enqueue,内部构造okhttp3.Call对象,执行对象的enqueue方法。然后在okhttp3$Callback方法中,调用retrofit2的 回调,赋值成功和失败。

  B:同步调用

  1、同理,同步调用,最后也是调用的。okhtt3.Call的execute方法。

  源码:https://github.com/square/retrofit

  引入:

implementation 'com.squareup.retrofit2:retrofit:2.0.2'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

andorid jar/库源码解析之retrofit2的更多相关文章

  1. andorid jar/库源码解析之Bolts

    目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...

  2. andorid jar/库源码解析之EventBus

    目录:andorid jar/库源码解析 EventBus: 作用: 用于不同Activity,Service等之间传递消息(数据). 栗子: A页面:onCreate定义   EventBus.ge ...

  3. andorid jar/库源码解析之Dagger/Dagger2

    目录:andorid jar/库源码解析 Dagger.Dagger2: 作用: 1.用于解耦Activity和业务逻辑 2.在使用业务的时候,不需要重复编写new代码. 3.当业务变化的时候,不需要 ...

  4. andorid jar/库源码解析之okhttp3

    目录:andorid jar/库源码解析 Okhttp3: 作用: 用于网络编程(http,https)的快速开发. 栗子: // okHttpClient定义成全局静态,或者单例,不然重复new可能 ...

  5. andorid jar/库源码解析之okio

    目录:andorid jar/库源码解析 Okio: 作用: 说白了,就是一个IO库,基于java原生io.来进行操作,内部做了优化,简洁,高效.所以受到了一部分人的喜欢和使用 栗子: 读写文件. p ...

  6. andorid jar/库源码解析之Butterknife

    目录:andorid jar/库源码解析 Butterknife: 作用: 用于初始化界面控件,控件方法,通过注释进行绑定控件和控件方法 栗子: public class MainActivity e ...

  7. andorid jar/库源码解析之zxing

    目录:andorid jar/库源码解析 Zxing: 作用: 生成和识别,二维码,条形码. 栗子: 生成二维码,赋值到ImageView上 QRCodeWriter qrCodeWriter = n ...

  8. andorid jar/库源码解析之错误提示

    目录:andorid jar/库源码解析 错误: 错误1: Error: Static interface methods are only supported starting with Andro ...

  9. andorid jar/库源码解析

    前言 本篇作为开篇,会大体上说明,需要解读源码的,类库,或者jar. 序 原本,类库和jar的系列准备写到逆向系列课程的,但是那个东西,在写了两篇,就没有后续了,现在也不知道从哪里开始了, 只能等后期 ...

随机推荐

  1. JUC——检视阅读

    JUC--检视阅读 参考资料 JUC知识图参考 JUC框架学习顺序参考 J.U.C学习总结参考,简洁直观 易百并发编程,实践操作1,不推荐阅读,不及格 JUC文章,带例子讲解,可以学习2 Doug L ...

  2. 多线程设置flag标志位实现同步

    信号灯解决同步问题 我尽量注释了代码,可以很容易理解了. package Thread; /** * 信号灯 * 借助标志位 */ public class FlagThread { public s ...

  3. AJ学IOS(41)UI之核心动画 两行代码搞定3D转场

    AJ分享,必须精品 效果: 代码: 其实代码很少,苹果都给封装好了 // 1.创建核心动画 CATransition *ca = [CATransition animation]; // 1.1动画过 ...

  4. G++编译链接的那些事

    语言 CPP 前言   虽然 VSCodeC++ 编辑器非常受大家的欢迎,无论是大佬还是小白都说对其爱不释手...   我...用了一段时间后发现实在是麻烦,配置往往花费我大量时间.可以说真的是吃力不 ...

  5. Jmeter连接mysql数据库?so easy!!!

    一.确保mysql数据库能够通过Navicat等远程连接工具连接. 注意:一定是确保能使用navicat连接,而不是dos窗口! 比如笔者需要查询ecshop数据库下的ecs_admin_user表, ...

  6. sublime text3配置html环境

    1.安装View in Browser 2.配置快捷键 [1]Preferences—Key Bindings—User. [2]插入代码 [ //ie { "keys": [&q ...

  7. L21 Momentum RMSProp等优化方法

    airfoil4755 下载 链接:https://pan.baidu.com/s/1YEtNjJ0_G9eeH6A6vHXhnA 提取码:dwjq 11.6 Momentum 在 Section 1 ...

  8. S - Primitive Primes CodeForces - 1316C 数学

    数学题 在f(x)和g(x)的系数里找到第一个不是p的倍数的数,然后相加就是答案 为什么? 设x1为f(x)中第一个不是p的倍数的系数,x2为g(x)...... x1+x2前的系数为(a[x1+x2 ...

  9. pytorch 中HWC转CHW

    import torch import numpy as np from torchvision.transforms import ToTensor t = torch.tensor(np.aran ...

  10. liunx系统二进制包安装编译mysql数据库

    liunx系统二进制包安装编译mysql数据库 # 解压二进制压缩包 [root@localhost ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C ...