Okhttp.Builder 中的 ExceptionInInitializerError w/ sslSocketFactory

ExceptionInInitializerError in Okhttp.Builder w/ sslSocketFactory

我正在开发一个项目,该项目需要对单个调用使用允许所有证书,每当我尝试设置 sslSocketFactory 时,我都会收到一条错误消息,指出 ExceptionInInitializerError。

我搜索了 SO 并找到了 this question, but it did not resolve the issue for me; same for this Git issue

我的示例代码如下:

    X509TrustManager trustManager = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] xcs, String string)
                throws CertificateException {}
        public void checkServerTrusted(X509Certificate[] xcs, String string)
                throws CertificateException {}
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };
    SSLContext sslContext;
    SSLSocketFactory sslSocketFactory = null;
    try {
        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, new TrustManager[]{trustManager}, null);
        sslSocketFactory = sslContext.getSocketFactory();
    } catch (Exception e){
        e.printStackTrace();
    }
    OkHttpClient client = new OkHttpClient.Builder().sslSocketFactory(sslSocketFactory,
            trustManager).addInterceptor(interceptor).build();

当发送的参数全部有效且不为空时,为什么我会收到错误消息?

事实证明,问题是由在 okhttp3 构建器方法中调用 NullPointerException 引起的。确切的代码在这里:

Caused by: java.lang.NullPointerException: Attempt to get length of null array
        at okhttp3.internal.tls.BasicTrustRootIndex.<init>(BasicTrustRootIndex.java:32)
        at okhttp3.internal.platform.Platform.buildTrustRootIndex(Platform.java:288)
        at okhttp3.internal.platform.AndroidPlatform.buildTrustRootIndex(AndroidPlatform.java:280)
        at okhttp3.internal.platform.Platform.buildCertificateChainCleaner(Platform.java:172)
        at okhttp3.internal.platform.AndroidPlatform.buildCertificateChainCleaner(AndroidPlatform.java:230)
        at okhttp3.internal.tls.CertificateChainCleaner.get(CertificateChainCleaner.java:41)
        at okhttp3.OkHttpClient$Builder.sslSocketFactory(OkHttpClient.java:694)

这个问题的原因是我遵循了 Whosebug (IE here) 上的大量示例,这些示例告诉您使用 return null; 行构建 x509 TrustManager。

解决方法是简单地更改一行:

    X509TrustManager trustManager = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] xcs, String string)
                throws CertificateException {}
        public void checkServerTrusted(X509Certificate[] xcs, String string)
                throws CertificateException {}
        public X509Certificate[] getAcceptedIssuers() {
            //Here
            return new X509Certificate[]{};
        }
    };
    .
    .
    .

问题就这样解决了。