主要分析以下幾個框架
- HttpClient
- HttpURLConnection
- Volley
- OkHttp
- Retrofit
網(wǎng)絡(luò)框架分類
底層網(wǎng)絡(luò)框架
HttpClient, HttpURLConnection屁商,OkHttp 屬于底層的網(wǎng)絡(luò)框架奈懒,這些框架是底層真正發(fā)起http請求的船殉。
封裝的網(wǎng)絡(luò)框架
Volley, retrofit屬于對底層網(wǎng)絡(luò)框架的封裝斟叼,比如Volley內(nèi)部使用的就是HttpURLConnection和HttpClient秤朗, retrofit是基于OkHttp 的封裝
大而全的框架
Afinal和xUtils等大而全的框架土陪,包括網(wǎng)絡(luò)框架昼汗,數(shù)據(jù)庫,圖片加載等鬼雀,不建議使用這類的框架顷窒,不好維護,并且這種框架在每個模塊都做的不精源哩。
HttpClient
介紹
HttpClient是Apache公司提供的原始的http協(xié)議庫鞋吉,在2.2之前,官方推薦使用HttpClient.
官方博客:
Android’s HTTP Clients
https://android-developers.googleblog.com/2011/09/androids-http-clients.html
(后面牽涉到HttpClient和HttpURLConnection的一部分分析的內(nèi)容來自這篇文章)
優(yōu)點
功能豐富励烦,非常穩(wěn)定谓着,bug極少
缺點
數(shù)量眾多的API使得在保證兼容的情況下很難改進,這也是Google官方放棄HttpClient的主要原因
發(fā)展
2.2之前HttpClient是最佳選擇坛掠,2.3之后赊锚, Google官方建議使用HttpURLConnection. Android6.0以后Google官方Api移除HttpClient
httpclient Android6.0問題及解決
問題:Android6.0以后Google官方Api移除HttpClient,繼續(xù)使用HttpClient及基于其封裝的網(wǎng)絡(luò)庫會出異常
解決:在 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協(xié)議庫却音,也是Java的標(biāo)準(zhǔn)類庫java.net中的一員
優(yōu)點
HttpURLConnection 是通用的輕量級的 HTTP 客戶端工具改抡,適用于大部分應(yīng)用程序。
增加了壓縮(2.3)和緩存(4.0)系瓢,減少了網(wǎng)絡(luò)使用阿纤,提高了速度,節(jié)省了電量
因為API較少夷陋,所以可以在保證兼容性的前提下做改進欠拾,這也是官方選擇HttpURLConnection取代HttpClient的主要原因
2.2之前存在的問題及解決
在 Android 2.2 之前的版本中胰锌,存在一些 bug;尤其是在可讀的輸入流中調(diào)用 close()藐窄,會污染連接池
解決:關(guān)閉連接池以保證正常使用
HttpClient 與HttpURLConnection资昧, Which client is best?
總結(jié):2.2之前HttpClient是最佳選擇,2.3之后荆忍,建議使用HttpURLConnection
Volley
介紹
2013年Google I/O大會上推出的網(wǎng)絡(luò)框架
Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. (不僅容易格带,而且更重要的是,更快)
https://developer.android.com/training/volley/index.html
優(yōu)點
(來自官方介紹)
- Automatic scheduling of network requests.(自動調(diào)度網(wǎng)絡(luò)請求)
- Multiple concurrent network connections.(多個并發(fā)網(wǎng)絡(luò)連接)
- Transparent disk and memory response caching with standard HTTP cache coherence.(透明的磁盤和內(nèi)存響應(yīng)緩存刹枉,并且具有標(biāo)準(zhǔn)的HTTP 緩存一致性)
- Support for request prioritization.(支持請求優(yōu)先級)
- Cancellation request API. You can cancel a single request, or you can set blocks or scopes of requests to cancel.(具備cancle請求的API,你可以cancel 一個請求叽唱,也可以設(shè)置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.(嚴(yán)格的有序化使得你可以容易的用網(wǎng)絡(luò)異步請求獲得的數(shù)據(jù)來正確的填充你的UI)
- Debugging and tracing tools.(調(diào)試和跟蹤工具)
適合
數(shù)據(jù)量不大棺亭,但通信頻繁的網(wǎng)絡(luò)操作,應(yīng)用中一般的網(wǎng)絡(luò)請求都屬于這種
不適合
Volley is not suitable for large download or streaming operations, since Volley holds all responses in memory during parsing.
不適合大的下載或者流式傳輸操作蟋软,因為Volley在解析過程中將所有響應(yīng)都存儲在內(nèi)存中镶摘。
Volley集成HttpClient和HttpURLConnection
在Android 2.3及以上的版本,使用的是HttpURLConnection岳守,而在Android 2.2及以下版本凄敢,使用的是HttpClient
HurlStack和HttpClientStack分別對應(yīng)HttpURLConnection 和HttpClient 。其實這兩個對象都是實現(xiàn)了HttpStack接口的
Volley集成OkHttp
同上湿痢,我們可以實現(xiàn)一個集成了OkHttp的HurlStack的實現(xiàn)
OkHttp作者Jake Wharton的Gist:
https://gist.github.com/JakeWharton/5616899
okhttp
介紹
okhttp是square公司開發(fā)的網(wǎng)絡(luò)框架贡未。
(后面會有文章詳細分析)
google官方使用
Android 4.4后,HttpURLConnection底層實現(xiàn)使用的就是OkHttp
優(yōu)點
(來自官方介紹)
- HTTP/2 support allows all requests to the same host to share a socket.(對同一個主機發(fā)出的所有請求都可以共享相同的套接字連接)
- Connection pooling reduces request latency (if HTTP/2 isn’t available).(使用連接池來復(fù)用連接以提高效率)
- Transparent GZIP shrinks download sizes.(提供了對 GZIP 的默認支持來降低傳輸內(nèi)容的大忻膳邸)
- Response caching avoids the network completely for repeat requests.(對 HTTP 響應(yīng)的緩存機制,可以避免不必要的網(wǎng)絡(luò)請求)
- 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(當(dāng)網(wǎng)絡(luò)出現(xiàn)問題時害幅,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是一個封裝的網(wǎng)絡(luò)框架岂昭,底層網(wǎng)絡(luò)框架使用的是OkHttp(Retrofit 2.0 開始不支持其他底層網(wǎng)絡(luò)框架)以现。
(后面會有文章詳細分析)
優(yōu)點
Retrofit是對http請求的更好的封裝,使用起來更簡單约啊,大大增加了開發(fā)效率邑遏。
其中的很多環(huán)節(jié)你都可以自定義,使用起來更靈活恰矩,比如你可以自定義json解析框架比如Gson记盒,Jackson等。
支持RxJava外傅,這也是很多人選擇它的原因纪吮。
RxJava+Retrofit+OkHttp
這基本上是目前網(wǎng)絡(luò)框架最好的選擇了俩檬。