andorid jar/库源码解析之retrofit2
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的更多相关文章
- andorid jar/库源码解析之Bolts
目录:andorid jar/库源码解析 Bolts: 作用: 用于链式执行跨线程代码,且传递数据 栗子: Task.call(new Callable<Boolean>() { @Ove ...
- andorid jar/库源码解析之EventBus
目录:andorid jar/库源码解析 EventBus: 作用: 用于不同Activity,Service等之间传递消息(数据). 栗子: A页面:onCreate定义 EventBus.ge ...
- andorid jar/库源码解析之Dagger/Dagger2
目录:andorid jar/库源码解析 Dagger.Dagger2: 作用: 1.用于解耦Activity和业务逻辑 2.在使用业务的时候,不需要重复编写new代码. 3.当业务变化的时候,不需要 ...
- andorid jar/库源码解析之okhttp3
目录:andorid jar/库源码解析 Okhttp3: 作用: 用于网络编程(http,https)的快速开发. 栗子: // okHttpClient定义成全局静态,或者单例,不然重复new可能 ...
- andorid jar/库源码解析之okio
目录:andorid jar/库源码解析 Okio: 作用: 说白了,就是一个IO库,基于java原生io.来进行操作,内部做了优化,简洁,高效.所以受到了一部分人的喜欢和使用 栗子: 读写文件. p ...
- andorid jar/库源码解析之Butterknife
目录:andorid jar/库源码解析 Butterknife: 作用: 用于初始化界面控件,控件方法,通过注释进行绑定控件和控件方法 栗子: public class MainActivity e ...
- andorid jar/库源码解析之zxing
目录:andorid jar/库源码解析 Zxing: 作用: 生成和识别,二维码,条形码. 栗子: 生成二维码,赋值到ImageView上 QRCodeWriter qrCodeWriter = n ...
- andorid jar/库源码解析之错误提示
目录:andorid jar/库源码解析 错误: 错误1: Error: Static interface methods are only supported starting with Andro ...
- andorid jar/库源码解析
前言 本篇作为开篇,会大体上说明,需要解读源码的,类库,或者jar. 序 原本,类库和jar的系列准备写到逆向系列课程的,但是那个东西,在写了两篇,就没有后续了,现在也不知道从哪里开始了, 只能等后期 ...
随机推荐
- 记一次pgsql中查询优化(子查询)
记一次pgsql的查询优化 前言 这是一个子查询的场景,对于这个查询我们不能避免子查询,下面是我一次具体的优化过程. 优化策略 1.拆分子查询,将需要的数据提前在cte中查询出来 2.连表查询,直接去 ...
- Codeup 25594 Problem H 例题5-8 Fibonacci数列
题目描述 输入一个正整数n,求Fibonacci数列的第n个数.Fibonacci数列的特点:第1,2个数为1,1.从第3个数开始,概述是前面两个数之和.即: 1,1,2,3,5,8,13,21 - ...
- 数据结构和算法(Golang实现)(1)简单入门Golang-前言
数据结构和算法在计算机科学里,有非常重要的地位.此系列文章尝试使用 Golang 编程语言来实现各种数据结构和算法,并且适当进行算法分析. 我们会先简单学习一下Golang,然后进入计算机程序世界的第 ...
- AJ学IOS(44)之网易彩票自定义图片在右边的Button_弹出view_ios6,7简单适配
AJ分享,必须精品 效果: 注意图里面了吗,其实那个效果做起来真的很简单,在iOS中苹果给我们封装的很好,关键是那个按钮 系统的按钮的图片是在左边的,这里我们需要把他调整到右边,然后呢需要我们自己做一 ...
- 基于my-DAQ的温室迷你温室设计
这是一个小项目,采用NI的my-DAQ做数据采集,需要采集的数据有温度(LM35),气体(MQ2),需要控制的设备有风扇.加热棒,另外还有光照亮度调节. 一.数据采集 1.LM35 LM35是模拟输出 ...
- IE各版本CSS Hack(兼容性处理)语法速查表
为了兼容IE各个版本,需要在CSS中添加额外的代码,比如以前常用的_width.之所以工作,是因为浏览器会忽略不能解析的样式规则,因此举个例子来说,把_width写在width下面,对于非IE浏览器会 ...
- unity3d的键盘和鼠标输入
一.键盘的输入 •GetKey,GetKeyDown,GetKeyUp三个方法分别获取用户键盘按键的输入 1. GetKey:用户长按按键有效: bool down = Input.GetKeyDow ...
- 王者荣耀英雄全皮肤4K高清大图,python爬虫帮你保存下来
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取t.cn ...
- 多窗体及窗体之间传值 以及listview的使用
三中打开窗口窗体状态: 1 messagebox.show 类型 特点: 从窗口form 1里打开另一个窗体form2,form2不关闭的情况下form1 不能操作:代码如下: private ...
- 详解PHP反序列化中的字符逃逸
首发先知社区,https://xz.aliyun.com/t/6718/ PHP 反序列化字符逃逸 下述所有测试均在 php 7.1.13 nts 下完成 先说几个特性,PHP 在反序列化时,对类中不 ...