okhttp3.8.1 学习笔记

58 篇文章 0 订阅
53 篇文章 1 订阅

okhttp3.8.1 学习笔记

概述

Request/Response

  • Request—->请求
    • An HTTP request. Instances of this class are immutable if their {@link #body} is null or itself immutable.
    • HttpUrl/method/headers/RequestBody—->包含请求的请求行、请求头、请求体
    • FormBody—->RequestBody(以application/x-www-form-urlencoded传递数据)
    • HttpEntityBody—->RequestBody(以application/octet-stream传递数据,适配httpcomponent-core的httpentity)
    • okhttp3.RequestBody#create(okhttp3.MediaType, java.lang.String)—->自定义头传递,eg:application/json
    • 请求体最终往okio.BufferedSink中写入请求体数据
  • Response—->响应
    • An HTTP response. Instances of this class are not immutable: the response body is a one-shot value that may be consumed only once and then closed. All other properties are immutable
    • code/Protocol/ResponseBody—->返回的状态码、协议、响应体
    • RealResponseBody—->ResponseBody
    • 从okio.BufferedSource中读取数据返回响应体

Call—->请求动作

  • A call is a request that has been prepared for execution. A call can be canceled. As this object represents a single request/response pair (stream), it cannot be executed twice
  • RealCall—->Call
    • okhttp3.RealCall#getResponseWithInterceptorChain
    • 委托给Interceptor.Chain执行请求

Interceptor.Chain—->请求/响应链支持

  • CallServerInterceptor—->Interceptor
  • RealInterceptorChain—->Interceptor.Chain
    • CallServerInterceptor—->位于拦截器链的最后一个,负责网络请求到对端网络,处理响应,均委托给HttpCodec进行处理
    • RealInterceptorChain—->提供运行整个拦截链机制,
    • 递归调用okhttp3.internal.http.RealInterceptorChain#proceed(okhttp3.Request, okhttp3.internal.connection.StreamAllocation, okhttp3.internal.http.HttpCodec, okhttp3.internal.connection.RealConnection)

HttpCodec—->请求/响应编解码支持

  • Encodes HTTP requests and decodes HTTP responses
  • Http2Codec—->HttpCodec
    • writeRequestHeaders/createRequestBody—->写请求头/请求体到Sink
    • readResponseHeaders/openResponseBody—->从Source中解析响应头/响应体

Sink/Source

  • BufferedSink—->Sink(输出缓存槽)
  • BufferedSource—->Source(输入缓存槽)

OkHttpClient—->okhttp入口

  • Factory for {@linkplain Call calls}, which can be used to send HTTP requests and read their responses.

StreamAllocation—->支持连接/请求/调用的一致性

  • okhttp3.internal.connection.StreamAllocation#newStream—->创建编解码器
  • okhttp3.internal.connection.StreamAllocation#findHealthyConnection—->使用连接池创建连接

测试

  • okhttp3.guide.GetExample
  • okhttp3.guide.PostExample
  • okhttp3.recipes.PostForm
  • okhttp3.recipes.RequestBodyCompression
  • okhttp3.sample.Crawler—->简单爬虫示例
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值