`
gaojingsong
  • 浏览: 1153324 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

【Android之OkHttp介绍】

阅读更多

OkHttp是一个第三方类库,用于android中请求网络。

 

这是一个开源项目,是安卓端最火热的轻量级框架,由移动支付Square公司贡献(该公司还贡献了Picasso和LeakCanary) 。用于替代HttpUrlConnection和Apache HttpClient(android API23 里已移除HttpClient)。

HTTP is the way modern applications network. It’s how we exchange data & media. Doing HTTP efficiently makes your stuff load faster and saves bandwidth.

OkHttp is an HTTP client that’s efficient by default:

1)HTTP/2 support allows all requests to the same host to share a socket.

2)Connection pooling reduces request latency (if HTTP/2 isn’t available).

3)Transparent GZIP shrinks download sizes.

4)Response caching avoids the network completely for repeat requests.

 

OkHttp优势

允许连接到同一个主机地址的所有请求,提高请求效率

共享Socket,减少对服务器的请求次数

通过连接池,减少了请求延迟 

缓存响应数据来减少重复的网络请求 

减少了对数据流量的消耗 

自动处理GZip压缩 

 

OkHttp perseveres when the network is troublesome: it will silently recover from common connection problems. If your service has multiple IP addresses OkHttp will attempt alternate addresses if the first connect fails. This is necessary for IPv4+IPv6 and for services hosted in redundant data centers. OkHttp initiates new connections with modern TLS features (SNI, ALPN), and falls back to TLS 1.0 if the handshake fails.

 

Using OkHttp is easy. Its request/response API is designed with fluent builders and immutability. It supports both synchronous blocking calls and async calls with callbacks.

 

OkHttp supports Android 2.3 and above. For Java, the minimum requirement is 1.7.

 

 

OkHttp功能

get,post请求

文件的上传下载

加载图片(内部会图片大小自动压缩)

支持请求回调,直接返回对象、对象集合

支持session的保持

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics