andorid jar/库源码解析之okhttp3
Okhttp3:
作用:
用于网络编程(http,https)的快速开发。
栗子:
// okHttpClient定义成全局静态,或者单例,不然重复new可能导致连接数耗尽
OkHttpClient okHttpClient = new OkHttpClient();
String url = "https://www.test.com";
byte[] data = new byte[] { 1 }; okhttp3.RequestBody body = okhttp3.RequestBody.create(MediaType.parse("application/octet-stream"), data); // Request
Request request = new Request.Builder().addHeader("Authorization", "Bearer XXXXXXXX").url(url).post(body).build(); // Response
Response response = okHttpClient.newBuilder().build().newCall(request).execute(); // 注意:这里是string不是toString
final String msg = response.body().string();
源码解读:

①:创建OkHttpClient对象,同时赋值默认值
②:返回一个 RequestBody对象,该对象包含,类型,长度,和写入数据的方法。
③:创建一个Request$Builder对象,默认使用GET请求,对addHeader进行添加到List<String>集合中,name,value.trim(),一个header用两条。
④:赋值请求地址,同时特殊处理ws->http,wss->https。对url进行拆分解析,.得到url中的schema,host,port,name,password,path等
⑤:赋值RequestBody和method成POST
⑥:用所有的Request$Builder成员,初始化一个Request对象。
⑦:用OkHttpClient对象的默认值,初始化一个OkHttpClient$Builder对象
⑧:返回一个OkHttpClient对象,值来自OkHttpClient$Builder
⑨:通过OkHttpClient和Request构造一个,RealCall对象。
⑩:调用RealCall的execute方法。a>把RealCall对象添加到,运行Call的集合中。b>创建 RealInterceptorChain 对象进行通讯。 c> 调用 proceed 方法。。d> 创建 List<Interceptor> 集合。循环调用 Interceptor的intercept方法,进行处理请求。的细节。
顺序: RetryAndFollowUpInterceptor、BridgeInterceptor、CacheInterceptor、ConnectInterceptor、networkInterceptors、CallServerInterceptor
最后在CallServerInterceptor 中的intercept中。执行创建一个 RealBufferedSink 对象,用于写入数据(post内容),然后调用finishRequest。
读取readResponseHeaders ,得到 Response.Builder 对象,使用这个对象,构造一个Response对象,把request,超时等信息,赋值到response上,判断response.code==100,重新readResponseHeaders,更新code的值。
调用responseHeadersEnd,完成读取同步,然后读取body:openResponseBody,得到 ResponseBody对象。赋值给Response对象,返回
⑪:得到ResponseBody对象而已,没啥说的
⑫:使用Okio 读取数据,并且返回(因为是流读取,所以只能调用一次)
源码:https://github.com/square/okhttp
引入:
implementation 'com.squareup.okhttp3:okhttp:3.12.1'
andorid jar/库源码解析之okhttp3的更多相关文章
- andorid jar/库源码解析之retrofit2
目录:andorid jar/库源码解析 Retrofit2: 作用: 通过封装okhttp库,来进行web通讯,并且使用动态代理的方式,来调用接口地址,通过回调赋值结果. 栗子: 定义一个接口,用于 ...
- 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/库源码解析之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的系列准备写到逆向系列课程的,但是那个东西,在写了两篇,就没有后续了,现在也不知道从哪里开始了, 只能等后期 ...
随机推荐
- java中取得用户输入的方法
java中取得用户输入的方法 1.采用java.util.Scanner类 采用Scannerd的next()方法读取,测试代码如下: Scanner sc=new Scanner(System.in ...
- stand up meeting 1/20/2016
part 组员 工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 修复bug 6 修复bug 6 foxit PDF ...
- work of weekend 12/12/2015~12/14/2015
part 组员 周末工作+今日工作 工作耗时/h 明日计划 工作耗时/h backup 冯晓云 try the backup plan:brower:rewrite bi ...
- Persona & User Scenario
Persona: Tom:男,21岁,大学生,周末经常和同学们一起出去吃饭.唱歌.打球.郊游,期间会时不时拍一些照片以作纪念,长期积累的照片数量较多且内容繁杂,很少对照片进行整理: Alisa:女,2 ...
- G - Number Transformation BFS
In this problem, you are given an integer number s. You can transform any integer number A to anothe ...
- TCP基础概念
定义 传输控制协议(TCP,Transmission Control Protocol)是一种面向连接的.可靠的.基于字节流的传输层通信协议 特点 TCP是一种面向广域网的通信协议,目的是在跨越多个网 ...
- api_DZFPKJ & api_DZFPCX(get_AES_url代码优化)
通过AES加密网站的接口来传值,不需要手动加密字符串后复制过来传值. #coding:utf-8 import requests import re def get_aes_url(key, text ...
- css 样式中 margin padding和top类定位的区别
1 margin margin 是外边距的意思,是边框到外部另一元素之间的距离,允许使用负值 语法结构: margin:5px auto; 意思上下为5,左右平均居中 ...
- CentOS 使用中问题记录
⚠️使用yum提示Error: rpmdb open failed的解决方案 清除原rpmdb文件,这一步可能不用操作,直接进行第2步 # rm -f /var/lib/rpm/__db.* 重建rp ...
- Python推荐系统框架:RecQ
RecQ是一个用于推荐系统的python库(python2.7.x),实现了一些state-of-the-art的推荐算法. github地址:https://github.com/Coder-Yu/ ...