android网络请求库比较

主流的网络请求库-介绍

如今Android中主流的网络请求框架有:

Android-Async-Http
Volley
OkHttp
Retrofit



主要分析以下几个框架

  1. HttpClient
  2. HttpURLConnection
  3. Volley
  4. OkHttp
  5. Retrofit

网络框架分类

底层网络框架

HttpClient, HttpURLConnection,OkHttp 属于底层的网络框架,这些框架是底层真正发起http请求的。

封装的网络框架

Volley, retrofit属于对底层网络框架的封装,比如Volley内部使用的就是HttpURLConnection和HttpClient, retrofit是基于OkHttp 的封装

扫描二维码关注公众号,回复: 2408822 查看本文章
大而全的框架

Afinal和xUtils等大而全的框架,包括网络框架,数据库,图片加载等,不建议使用这类的框架,不好维护,并且这种框架在每个模块都做的不精。

HttpClient

介绍

HttpClient是Apache公司提供的原始的http协议库,在2.2之前,官方推荐使用HttpClient.

官方博客:
Android’s HTTP Clients
https://android-developers.googleblog.com/2011/09/androids-http-clients.html
(后面牵涉到HttpClient和HttpURLConnection的一部分分析的内容来自这篇文章)

优点

功能丰富,非常稳定,bug极少

官方博客截图.png
缺点

数量众多的API使得在保证兼容的情况下很难改进,这也是Google官方放弃HttpClient的主要原因

官方博客截图.png
发展

2.2之前HttpClient是最佳选择,2.3之后, Google官方建议使用HttpURLConnection. Android6.0以后Google官方Api移除HttpClient

httpclient Android6.0问题及解决

问题:Android6.0以后Google官方Api移除HttpClient,继续使用HttpClient及基于其封装的网络库会出异常
解决:在 build.gradle 文件中声明以下编译时依赖项:
android {
useLibrary ‘org.apache.http.legacy’
}

参考:官方介绍
Android 6.0 变更
https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

HttpURLConnection

介绍

HttpURLConnection是Sun公司提供的原始的http协议库,也是Java的标准类库java.net中的一员

优点

HttpURLConnection 是通用的轻量级的 HTTP 客户端工具,适用于大部分应用程序。
增加了压缩(2.3)和缓存(4.0),减少了网络使用,提高了速度,节省了电量
因为API较少,所以可以在保证兼容性的前提下做改进,这也是官方选择HttpURLConnection取代HttpClient的主要原因

官方博客截图.png
2.2之前存在的问题及解决

在 Android 2.2 之前的版本中,存在一些 bug;尤其是在可读的输入流中调用 close(),会污染连接池
解决:关闭连接池以保证正常使用

官方博客截图.png

HttpClient 与HttpURLConnection, Which client is best?

官方博客截图.png

总结:2.2之前HttpClient是最佳选择,2.3之后,建议使用HttpURLConnection

Volley

介绍

2013年Google I/O大会上推出的网络框架
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. (不仅容易,而且更重要的是,更快)

https://developer.android.com/training/volley/index.html

优点

(来自官方介绍)

  • Automatic scheduling of network requests.(自动调度网络请求)
  • Multiple concurrent network connections.(多个并发网络连接)
  • Transparent disk and memory response caching with standard HTTP cache coherence.(透明的磁盘和内存响应缓存,并且具有标准的HTTP 缓存一致性)
  • Support for request prioritization.(支持请求优先级)
  • Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.(具备cancle请求的API,你可以cancel 一个请求,也可以设置cancle 的块和范围)
  • Ease of customization, for example, for retry and backoff.(可以轻松的定制,比如,重试机制和回退机制)
  • Strong ordering that makes it easy to correctly populate your UI with data fetched asynchronously from the network.(严格的有序化使得你可以容易的用网络异步请求获得的数据来正确的填充你的UI)
  • Debugging and tracing tools.(调试和跟踪工具)
适合

数据量不大,但通信频繁的网络操作,应用中一般的网络请求都属于这种

不适合

Volley is not suitable for large download or streaming operations, since Volley holds all responses in memory during parsing.
不适合大的下载或者流式传输操作,因为Volley在解析过程中将所有响应都存储在内存中。

Volley集成HttpClient和HttpURLConnection

在Android 2.3及以上的版本,使用的是HttpURLConnection,而在Android 2.2及以下版本,使用的是HttpClient
HurlStack和HttpClientStack分别对应HttpURLConnection 和HttpClient 。其实这两个对象都是实现了HttpStack接口的

Volley集成OkHttp

同上,我们可以实现一个集成了OkHttp的HurlStack的实现
OkHttp作者Jake Wharton的Gist:
https://gist.github.com/JakeWharton/5616899

okhttp

介绍

okhttp是square公司开发的网络框架。
(后面会有文章详细分析)

google官方使用

Android 4.4后,HttpURLConnection底层实现使用的就是OkHttp

优点

(来自官方介绍)

  • HTTP/2 support allows all requests to the same host to share a socket.(对同一个主机发出的所有请求都可以共享相同的套接字连接)
  • Connection pooling reduces request latency (if HTTP/2 isn’t available).(使用连接池来复用连接以提高效率)
  • Transparent GZIP shrinks download sizes.(提供了对 GZIP 的默认支持来降低传输内容的大小)
  • Response caching avoids the network completely for repeat requests.(对 HTTP 响应的缓存机制,可以避免不必要的网络请求)
  • 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 会自动重试一个主机的多个 IP 地址)
  • 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.(支持Android2.3+ java 1.7+)

Retrofit

介绍

Retrofit与okhttp一样出自于Square公司。Retrofit是一个封装的网络框架,底层网络框架使用的是OkHttp(Retrofit 2.0 开始不支持其他底层网络框架)。
(后面会有文章详细分析)

优点

Retrofit是对http请求的更好的封装,使用起来更简单,大大增加了开发效率。
其中的很多环节你都可以自定义,使用起来更灵活,比如你可以自定义json解析框架比如Gson,Jackson等。
支持RxJava,这也是很多人选择它的原因。

RxJava+Retrofit+OkHttp

这基本上是目前网络框架最好的选择了。



作者:野生的安卓兽
链接:https://www.jianshu.com/p/86fff768b063
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

猜你喜欢

转载自blog.csdn.net/xinzhou201/article/details/80902428