【OkHttp】OkHttp 源码分析 ( 网络框架封装 | OkHttp 4 迁移 | OkHttp 建造者模式 )

文章目录

OkHttp 系列文章目录

一、网络框架封装

二、OkHttp 4 迁移

三、OkHttp 建造者模式

四、博客资源





一、网络框架封装


HttpURLConnection 是 Android 中原生的 Http 网络请求 API , 在 SDK 中提供 , 是 Google 官方提供的 Http 网络框架 , 使用起来很繁琐 ;



Http 网络框架的作用是封装 Http 请求 ( Request ) 和 响应 ( Response ) ;


Request 请求封装 : 中封装 请求头 , 参数 , 将下面的参数封装到 Request 请求中 ;


GET / HTTP/1.1
Host: rucfd.ruc.edu.cn
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
If-None-Match: "b7f-5b575595fe630-gzip"
If-Modified-Since: Wed, 02 Dec 2020 06:28:18 GMT



Response 响应封装 : 将 响应头 以及各种响应参数 封装到 Request 响应中 ;


HTTP/1.1 200 OK
Server: none
Date: Mon, 21 Jun 2021 05:15:56 GMT
Content-Type: text/html
Content-Length: 1104
Connection: keep-alive
Last-Modified: Wed, 02 Dec 2020 06:28:18 GMT
ETag: "b7f-5b575595fe630-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip


OkHttp 网络框架就实现了上述封装 , 并且用法更加






二、OkHttp 4 迁移


OkHttp3 使用 Java 实现 , OkHttp4 使用 Kotlin 实现 ;


如果迁移到 OkHttp4 , 原则上不会出现错误 , 二者肯定是兼容的 , 原有代码不用进行修改 ;


还是有一些需要进行兼容的地方 , 参考如下页面 ;


https://square.github.io/okhttp/upgrading_to_okhttp_4/



【OkHttp】OkHttp 源码分析 ( 网络框架封装 | OkHttp 4 迁移 | OkHttp 建造者模式 )





三、OkHttp 建造者模式


使用 OkHttp 创建 Request 请求对象时 , 使用到了 " 建造者模式 " ;


Request request = new Request.Builder()

       .url("https://www.baidu.com")   // 设置请求地址

       .get()                          // 使用 Get 方法

       .build();

1

2

3

4

Request 中需要设置很多成员变量需要初始化 , 大部分参数都不是必须的 , 在构造函数中不可能设置十几个甚至几十个参数 ;

因此这里使用 " 建造者设计模式 " , 可以根据自己的需求 , 为类设置一个 " 建造者 " Builder , 用户通过该 Builder 对象根据需求配置不同的参数 ;


在 new Request.Builder() 建造者的构造函数中 , 对 Request 进行了默认初始化操作 ;



Request.Builder 代码示例 :


 public static class Builder {

   @Nullable HttpUrl url;

   String method;

   Headers.Builder headers;

   @Nullable RequestBody body;


   /** A mutable map of tags, or an immutable empty map if we don't have any. */

   Map<Class<?>, Object> tags = Collections.emptyMap();


   public Builder() {

     this.method = "GET";

     this.headers = new Headers.Builder();

   }

   

   Builder(Request request) {

     this.url = request.url;

     this.method = request.method;

     this.body = request.body;

     this.tags = request.tags.isEmpty()

         ? Collections.emptyMap()

         : new LinkedHashMap<>(request.tags);

     this.headers = request.headers.newBuilder();

   }


   public Builder url(HttpUrl url) {

     if (url == null) throw new NullPointerException("url == null");

     this.url = url;

     return this;

   }

 }


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30



上一篇:jsp response对象


下一篇:【2019 云栖大会】对象存储海量数据管理和应用最佳实践