Android 開發中常用的開源項目及比較 - 網絡篇
由于xUtils公共的庫它不是只支持http,還支持orm、bitmap、view注解,本著專一原則我們項目中就不會選擇xUtils, 這里我們就不介紹它了。
下面我們就分別說說Retrofit、okhttp、android-async-http、還要volley。
Volley
Google提供網絡通信庫,在2013年Google I/O大會上推出了一個新的網絡通信框架,官方是這樣說的:
Easy, Fast Networking for Android
特點:
-
自動調度網絡請求
-
多線程并發網絡連接、請求優先級
-
請求Cache和內存管理
-
擴展性強 如:支持自定義重連等
-
支持請求取消
-
強大的Debug、tracing
缺點:
-
對于大數據量的網絡操作糟糕, 如:下載文件等
下圖是它的工作原理圖
它的設計目標就是非常適合去進行數據量不大,但通信頻繁的網絡操作,如list加載等等
項目地址: https://android.googlesource.com/platform/frameworks/volley
技術文檔:
http://commondatastorage.googleapis.com/io-2013/presentations/110%20-%20Volley-%20Easy,%20Fast%20Networking%20for%20Android.pdf
okhttp
square 開源的 http協議 工具類,Android系統API 19以后HttpURLConnection內部實現就是使用了okhttp
特點:
-
支持Http/2,Http/2主要支持 SPDY( http://zh.wikipedia.org/wiki/SPDY )協議。SPDY 協議是 Google 開發的基于傳輸控制協議的應用層協議,通過壓縮,多路復用(一個 TCP 鏈接傳送網頁和圖片等資源)和優先級來縮短加載時間。
-
如果 Http/2 不可用,利用連接池減少請求延遲
-
Gzip 壓縮
-
Response 緩存減少不必要的請求
-
支持請求取消
缺點:
在服務器不支持speedy的情況下沒有特別明顯的優化
工作原理圖
項目地址:
https://github.com/square/okhttp
Retrofit
Square 開源RESTFUL API庫, Retrofit的跟Volley是一個套路,但解耦的更徹底。同時自己內部對OkHtttp客戶端做了封裝, 用Retrofit+OkHttp基本上已經可以處理任何業務場景了。
特點:
-
簡化了網絡請求流程,支持注解請求
-
支持多種Converter、還可以自定義, 如:Gson、Jackson、protobuf、xml
-
可以配合RxJava使用
缺點:
看不出缺點是最大的缺點
下面的工作原理圖是網友Stay的作品,如下:
項目地址:
https://github.com/square/retrofit
android-async-http
由于HttpClient在Android API 23后就不能使用了, android-async-http內部實現了HttpClient。 這個庫我也沒有用過, 只是看過一些介紹,下面摘抄了一下官網的說明僅供參考
-
Make asynchronous HTTP requests, handle responses in anonymous callbacks
-
HTTP requests happen outside the UI thread
-
Requests use a threadpool to cap concurrent resource usage
-
GET/POST params builder (RequestParams)
-
Multipart file uploads with no additional third party libraries
-
Tiny size overhead to your application, only 60kb for everything
-
Automatic smart request retries optimized for spotty mobile connections
-
Automatic gzip response decoding support for super-fast requests
-
Optional built-in response parsing into JSON (JsonHttpResponseHandler)
-
Optional persistent cookie store, saves cookies into your app’s SharedPreferences
項目地址: https://github.com/loopj/android-async-http
總結
網絡框架就介紹這么多吧, 以后還有合適的再做更新吧, 希望大家使用愉快!
來自:http://mp.weixin.qq.com/s?__biz=MzAwOTY0MTc3OA==&mid=2650253330&idx=1&sn=c3aa516fc915f82da78e81ee31f3502e#rd