android application http arch
Http 框架調(diào)查分析
概述 app的http提供的是什么有的服務,對這些lib有哪些的性能要求,會容易出現(xiàn)什么錯誤?
android中里各個部分處理都有一些成熟的框架趴泌,如在博客里作者的分析一樣堡牡,這里主要分析網(wǎng)絡(luò)請求的部分午绳。
android里的網(wǎng)絡(luò)連接主要是socket和http筒溃。但http使用較多马篮。
1 android 網(wǎng)絡(luò)請求框架概述
1.1 較底層的封裝
如okhttp,基于http協(xié)議封裝的一套客戶端铡羡,面向真正的請求积蔚;
httpClient,提供支持http協(xié)議的客戶端編程工具包烦周,實現(xiàn)了所有http方法,如GET POST等怎顾;
HttpUrlConnection
1.1.1 httpClient分析
HttpClient是Apache Jakarta Common下的子項目读慎,用來提供高效的、最新的槐雾、功能豐富的支持HTTP協(xié)議的客戶端編程工具包夭委,并且它支持HTTP協(xié)議最新的版本和建議。HttpClient已經(jīng)應用在很多的項目中募强,比如Apache Jakarta上很著名的另外兩個開源項目Cactus和HTMLUnit都使用了HttpClient株灸。
下載地址: http://hc.apache.org/downloads.cgi
一個get的例子:
HttpGet httpGet = new HttpGet("http://www.baidu.com");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println("Response content: " + EntityUtils.toString(entity));
}
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
1.1.2 okHttp分析
square實現(xiàn)的庫,高效擎值,支持SPDY慌烧,連接池、GZIP和 HTTP 緩存鸠儿。默認情況下屹蚊,OKHttp會自動處理常見的網(wǎng)絡(luò)問題,像二次連接进每、SSL的握手問題汹粤。支持同步,異步田晚,
一個簡單get的例子:
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
1.2 較高層的封裝
1.2.1 volley
內(nèi)部基于HttpUrlConnection嘱兼,HttpClient,OkHttp
google自己的贤徒,適合數(shù)據(jù)量不大芹壕,通信頻繁的網(wǎng)絡(luò)操作汇四。不支持同步,不能post大數(shù)據(jù)哪雕,異步庫船殉,
volley提供的功能:
- json 圖像的異步下載
- 網(wǎng)絡(luò)請求的排序
- 網(wǎng)絡(luò)請求的優(yōu)先級處理
- 緩存
- 多級別的取消請求
- 與Activity的交互
volley的架構(gòu)設(shè)計:
基礎(chǔ)結(jié)構(gòu)為線程池,分為主線程斯嚎,cache線程利虫,network線程。
get 的例子:
private void loadGetStr(String url) {
StringRequest srReq = new StringRequest(Request.Method.GET, url,
new StrListener(), new StrErrListener()) {
protected final String TYPE_UTF8_CHARSET = "charset=UTF-8";
// 重寫parseNetworkResponse方法改變返回頭參數(shù)解決亂碼問題
// 主要是看服務器編碼堡僻,如果服務器編碼不是UTF-8的話那么就需要自己轉(zhuǎn)換糠惫,反之則不需要
@Override
protected Response<String> parseNetworkResponse(
NetworkResponse response) {
try {
String type = response.headers.get(HTTP.CONTENT_TYPE);
if (type == null) {
type = TYPE_UTF8_CHARSET;
response.headers.put(HTTP.CONTENT_TYPE, type);
} else if (!type.contains("UTF-8")) {
type += ";" + TYPE_UTF8_CHARSET;
response.headers.put(HTTP.CONTENT_TYPE, type);
}
} catch (Exception e) {
}
return super.parseNetworkResponse(response);
}
};
srReq.setShouldCache(true); // 控制是否緩存
startVolley(srReq);
}
源碼分析:
arch:RequestQueue->Dispatch Thread ->Get Data Interface -> Data
RequestQueue: StringRequest, JsonRequest,ImageRequest , ...
DispatchTread: cacheDispatcher, NetworkDispatcher
...
volley: API user via RequestQueue to launch a request queue, whitch contains the request need to send.
Request: base class for all network requests.
RequestQueue: 請求分發(fā)隊列,
cacheDispatcher: 處理緩存的請求的線程
NetworkDispatcher: 處理網(wǎng)絡(luò)請求的線程
1.2.2 androi-async-http
內(nèi)部基于httpClient钉疫,異步庫硼讽,android現(xiàn)在不推薦使用httpClient
1.2.3 liteHttp
1.2.4 Retrofit
開發(fā)者是square,類似于volley的更高層的封裝庫牲阁,默認下面是okhttp固阁。Retrofit簡化了網(wǎng)絡(luò)請求的流程,提供不同的Json Converter實現(xiàn)城菊,提供RxJava支持备燃,還有Dagger2.比volley解耦更徹底,
retrofit提供的功能:
- 配置不同的http client
- 配置不同的反序列化工具來進行網(wǎng)絡(luò)解析
分析的links:http://www.reibang.com/p/45cb536be2f4