Volley 已經(jīng)發(fā)布很長(zhǎng)時(shí)間了, 也已被廣泛應(yīng)用, 相關(guān)教程到處都是. 本文只說(shuō)兩個(gè)值得注意的地方.
本文講解部分比較少, 請(qǐng)參閱提供的相關(guān)鏈接. 完整的實(shí)現(xiàn)代碼在 [Github dodocat/AndroidNetworkDemo] 可能看起來(lái)比這里更清晰.
使用 OkHttp 作為傳輸層的實(shí)現(xiàn).
Volley 默認(rèn)根據(jù) Android 系統(tǒng)版本使用不同的 Http 傳輸協(xié)議實(shí)現(xiàn).
在 Android 2.3以下使用 ApacheHttpStack 作為傳輸協(xié)議, 在 3.0 及以下使用 HttpURLConnection 作為傳輸層協(xié)議 (感謝評(píng)論中指正的朋友).
OkHttp 相較于其它的實(shí)現(xiàn)有以下的優(yōu)點(diǎn).
- 支持SPDY,允許連接同一主機(jī)的所有請(qǐng)求分享一個(gè)socket。
- 如果SPDY不可用,會(huì)使用連接池減少請(qǐng)求延遲。
- 使用GZIP壓縮下載內(nèi)容,且壓縮操作對(duì)用戶是透明的。
- 利用響應(yīng)緩存來(lái)避免重復(fù)的網(wǎng)絡(luò)請(qǐng)求。
- 當(dāng)網(wǎng)絡(luò)出現(xiàn)問(wèn)題的時(shí)候,OKHttp會(huì)依然有效,它將從常見(jiàn)的連接問(wèn)題當(dāng)中恢復(fù)。
- 如果你的服務(wù)端有多個(gè)IP地址,當(dāng)?shù)谝粋€(gè)地址連接失敗時(shí),OKHttp會(huì)嘗試連接其他的地址,這對(duì)IPV4和IPV6以及寄宿在多個(gè)數(shù)據(jù)中心的服務(wù)而言,是非常有必要的。
因此使用 OkHttp 作為替代是好的選擇.
-
首先用 OkHttp 實(shí)現(xiàn)一個(gè)新的
HurlStack用于構(gòu)建 Volley 的 requestQueue.
public class OkHttpStack extends HurlStack {
private OkHttpClient okHttpClient;
/**
* Create a OkHttpStack with default OkHttpClient.
*/
public OkHttpStack() {
this(new OkHttpClient());
}
/**
* Create a OkHttpStack with a custom OkHttpClient
* @param okHttpClient Custom OkHttpClient, NonNull
*/
public OkHttpStack(OkHttpClient okHttpClient) {
this.okHttpClient = okHttpClient;
}
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
return okUrlFactory.open(url);
}
}
1. 然后使用 OkHttpStack 創(chuàng)建新的 Volley requestQueue.
``` java
requestQueue = Volley.newRequestQueue(getContext(), new OkHttpStack());
requestQueue.start();
這樣就行了.
使用 Https
作為一個(gè)有節(jié)操的開(kāi)發(fā)者應(yīng)該使用 Https 來(lái)保護(hù)用戶的數(shù)據(jù), Android 開(kāi)發(fā)者網(wǎng)站上文章[Security with HTTPS and SSL]做了詳盡的闡述.
OkHttp 自身是支持 Https 的. 參考文檔 [OkHttp Https], 直接使用上面的 OkHttpStack 就可以了, 但是如果遇到服務(wù)器開(kāi)發(fā)哥哥使用了自簽名的證書(shū)(不要問(wèn)我為什么要用自簽名的), 就無(wú)法正常訪問(wèn)了.
網(wǎng)上有很多文章給出的方案是提供一個(gè)什么事情都不做的TrustManager 跳過(guò) SSL 的驗(yàn)證, 這樣做很容受到攻擊, Https 也就形同虛設(shè)了.
我采用的方案是將自簽名的證書(shū)打包入 APK 加入信任.
好處:
- 應(yīng)用難以逆向, 應(yīng)用不再依賴系統(tǒng)的 trust store, 使得 Charles 抓包等工具失效. 要分析應(yīng)用 API 必須反編譯 APK.
- 不用額外購(gòu)買證書(shū), 省錢....
缺點(diǎn):
- 證書(shū)部署靈活性降低, 一旦變更證書(shū)必須升級(jí)程序.
實(shí)現(xiàn)步驟
以最著名的自簽名網(wǎng)站12306為例說(shuō)明
-
導(dǎo)出證書(shū)
echo | openssl s_client -connect kyfw.12306.cn:443 2>&1 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > kyfw.12306.cn.pem -
將證書(shū)轉(zhuǎn)為 bks 格式
下載最新的bcprov-jdk, 執(zhí)行下面的命令. storepass 是導(dǎo)出密鑰文件的密碼.
export CERTSTORE=kyfw.12306.cn.bks
keytool -importcert -v
-trustcacerts
-alias 0
-file <(openssl x509 -in kyfw.12306.cn.pem)
-keystore $CERTSTORE -storetype BKS
-providerclass org.bouncycastle.jce.provider.BouncyCastleProvider
-providerpath ./bcprov-jdk16-1.46.jar
-storepass asdfqaz
```
將導(dǎo)出的 kyfw.bks 文件放入 res/raw 文件夾下.
-
創(chuàng)建
SelfSignSslOkHttpStack
/**
-
A HttpStack implement witch can verify specified self-signed certification.
*/
public class SelfSignSslOkHttpStack extends HurlStack {private OkHttpClient okHttpClient;
private Map<String, SSLSocketFactory> socketFactoryMap;
/**
- Create a OkHttpStack with default OkHttpClient.
*/
public SelfSignSslOkHttpStack(Map<String, SSLSocketFactory> factoryMap) {
this(new OkHttpClient(), factoryMap);
}
/**
- Create a OkHttpStack with a custom OkHttpClient
- @param okHttpClient Custom OkHttpClient, NonNull
*/
public SelfSignSslOkHttpStack(OkHttpClient okHttpClient, Map<String, SSLSocketFactory> factoryMap) {
this.okHttpClient = okHttpClient;
this.socketFactoryMap = factoryMap;
}
@Override
protected HttpURLConnection createConnection(URL url) throws IOException {
if ("https".equals(url.getProtocol()) && socketFactoryMap.containsKey(url.getHost())) {
HttpsURLConnection connection = (HttpsURLConnection) new OkUrlFactory(okHttpClient).open(url);
connection.setSSLSocketFactory(socketFactoryMap.get(url.getHost()));
return connection;
} else {
return new OkUrlFactory(okHttpClient).open(url);
}
}
} - Create a OkHttpStack with default OkHttpClient.
-
然后用
SelfSignSslOkHttpStack創(chuàng)建 Volley 的 RequestQueue.String[] hosts = {"kyfw.12306.cn"}; int[] certRes = {R.raw.kyfw}; String[] certPass = {"asdfqaz"}; socketFactoryMap = new Hashtable<>(hosts.length); for (int i = 0; i < certRes.length; i++) { int res = certRes[i]; String password = certPass[i]; SSLSocketFactory sslSocketFactory = createSSLSocketFactory(context, res, password); socketFactoryMap.put(hosts[i], sslSocketFactory); } HurlStack stack = new SelfSignSslOkHttpStack(socketFactoryMap); requestQueue = Volley.newRequestQueue(context, stack); requestQueue.start(); -
我們來(lái)試一試, 用上一步穿件的 RequestQueue 替換掉原來(lái)的, 然后發(fā)請(qǐng)求試試.
StringRequest request = new StringRequest( Request.Method.GET, "https://kyfw.12306.cn/otn/", new Response.Listener<String>() { @Override public void onResponse(String response) { responseContentTextView.setText(response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { responseContentTextView.setText(error.toString()); } }); RequestManager.getInstance(this).addRequest(request, this);
1. done
[Volley]:http://developer.android.com/training/volley/index.html
[OkHttp]:http://square.github.io/okhttp/
[Gson]:https://github.com/google/gson
[Security with HTTPS and SSL]:https://developer.android.com/training/articles/security-ssl.html
[OkHttp Https]:https://github.com/square/okhttp/wiki/HTTPS
[Github dodocat/AndroidNetworkDemo]:https://github.com/dodocat/AndroidNetworkdemo