我們在一些項目中使用類似的SocketFactory
和X509TrustManager
實現。爲了將這些類綁定到您的實現上,您基本上必須要做的就是將它們連接到用於客戶端 - 服務器通信的HttpClient
(假設這就是您正在使用的)。
我們通常有一種方法可以用上面提到的工廠和信任管理器創建一個HttpClient
。它看起來有點像這樣,並且基於內聯評論中顯示的鏈接鬆散。
protected HttpClient constructClient() {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
// use the debug proxy to view internet traffic on the host computer
if (ABCApplication.isDebuggingProxy()) params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(ABCConstants.DEBUG_PROXY_HOST, ABCConstants.DEBUG_PROXY_PORT, "http"));
// ignore ssl certification (due to signed authority not appearing on android list of permitted authorities)
// see: http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", new PlainSocketFactory(), 80));
registry.register(new Scheme("https", new FakeSocketFactory(), 443));
ClientConnectionManager cm = new SingleClientConnManager(params, registry);
return new DefaultHttpClient(cm, params);
}
希望能幫助您的實施。
這不只是幫助,它只是工作!它看起來和我嘗試過的所有其他人一樣,但他們增加了更多的參數。他們似乎也喜歡使用ThreadSafeClientConnManager而不是Single。當我有時間時,我會教育自己的差異。感謝名單! (如果我有代表,我會+1你的答案)。 – MBro 2012-04-28 18:22:30