在遗留代码库上工作仍然坚持使用过时的Apache HttpClient (在“下一版本之后”的路线图中将摆脱它)。在扩展DefaultHttpClient的类中使用通过重写createClientConnectionManager()实现的证书锁定
public class CustomHttpClient extends DefaultHttpClient {
public CustomHttpClient() {
// calling no-arg super ctor implicitly
}
@Override
protected ClientConnectionManager createClientConnectionManager() {
// return ThreadSafeClientConnManager
// using a SchemeRegistry for https port 443
// with certificate-pinning SSLSocketFactory for https port 443
}这在使用compileSdkVersion 23和useLibrary 'org.apache.http.legacy'的调试版本上工作得很好。这在没有遗留支持库的较小编译SDK版本上的调试和发布构建上都能很好地工作。
这在使用compileSdkVersion 23和遗留支持库的发布版本上不起作用。未调用createClientConnectionManager()。
为发布版本启用Proguard,其设置遵循SDK proguard-android-optimize.txt,并为传统支持库添加了以下内容:
-dontwarn org.apache.http.**添加-dontoptimize没有效果。
使用minifyEnabled false禁用proguard会使重写再次被调用。然而,禁用proguard对我来说不是一个选择。
我知道一个解决此问题的方法。有兴趣了解根本原因,也许还有更好的解决办法。
发布于 2015-10-07 21:52:00
通过使createClientConnectionManager()成为从构造函数调用的静态助手,并显式地传递给超级构造函数,解决了这个问题:
public class CustomHttpClient extends DefaultHttpClient {
public CustomHttpClient() {
super(crateClientConnectionManager(), null);
}
private static ClientConnectionManager createClientConnectionManager() {
// return ThreadSafeClientConnManager
// using a SchemeRegistry for https port 443
// with certificate-pinning SSLSocketFactory for https port 443
}现在,在所有配置中都使用了自定义连接管理器。
https://stackoverflow.com/questions/32994068
复制相似问题