wechatpay-apiv3/wechatpay-apache-httpclient

Android 10.0 创建httpClient时第三方包 org.apache.httpcomponents:httpclient 的 AllowAllHostnameVerifier 与系统的有冲突。

kbc2019 opened this issue · 2 comments

出现问题的log:
java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; in class Lorg/apache/http/conn/ssl/AllowAllHostnameVerifier; or its superclasses (declaration of 'org.apache.http.conn.ssl.AllowAllHostnameVerifier' appears in /system/framework/framework.jar!classes3.dex)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.(SSLConnectionSocketFactory.java:151)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:977)
at com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder.build(WechatPayHttpClientBuilder.java:88)
at com.example.myapplication.MainActivity.sendHttpRequest2(MainActivity.java:250)
at com.example.myapplication.MainActivity.access$000(MainActivity.java:64)
at com.example.myapplication.MainActivity$2.run(MainActivity.java:186)
at java.lang.Thread.run(Thread.java:919)

代码片段如下:
X509Certificate certificate = getCertificate(CERT_PATH + "wechatpay.pem");
List x509CertificateList = new ArrayList<>();
x509CertificateList.add(certificate);

    PrivateKey privateKey = getPrivateKey(CERT_PATH + "apiclient_key.pem");

    WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
            .withMerchant(mch_id, mchSerialNo, privateKey)
            .withWechatPay(x509CertificateList);

    CloseableHttpClient httpClient = null;
    try {
        httpClient = builder.build();
    } catch (IllegalArgumentException e) {
        Log.e("ERROR", "IllegalArgumentException =" + e.getMessage());
    }

系统默认找framework里的AllowAllHostnameVerifier , 但里面没有静态的INSTANCE;而第三方包org.apache.httpcomponents:httpclient里是有的。有人碰到同样的问题吗?请问如何解决。

一样遇见了,这里有这个问题的描述:https://blog.csdn.net/divaid/article/details/121154062
等版本更新。

// 获取证书管理器实例
certificatesManager = CertificatesManager.getInstance();
// 向证书管理器增加需要自动更新平台证书的商户信息
certificatesManager.putMerchant(merchantId, new WechatPay2Credentials(merchantId,
        new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
        apiV3Key.getBytes(StandardCharsets.UTF_8));
// 从证书管理器中获取verifier
verifier = certificatesManager.getVerifier(merchantId);

感谢!用cz.msebera.android:httpclient:4.5.8 替换apache的包就可以正常跑了。
可能是为了兼容除Android以外的其他Java开发而继续使用apache的包。