java.lang.IllegalStateException: Unable to extract the trust manager on Android10Platform, sslSocketFactory is class com.android.org.conscrypt.OpenSSLSocketFactoryImpl
2024-04-01 17:56:38.044 23697-25362 System.err com.xxx. W at okhttp3.OkHttpClient$Builder.sslSocketFactory(OkHttpClient.kt:810)
2024-04-01 17:56:38.044 23697-25362 System.err com.xxx. W at com.xxx..http.HttpHelper.getClient(HttpHelper.java:73)
2024-04-01 17:56:38.044 23697-25362 System.err com.xxx. W at com.xxx..http.HttpHelper.doRequest(HttpHelper.java:110)
1.最近由于升级Android Studio以及 sdk版本 导致上传报告使用okhttp报错如上面代码所示,于是我查看源码分析一波发现问题出现在 在 OkHttpClient 构建方法中 于是尝试修改代码
2.修改之前代码
/**
* get okhttpclient
* @param timeOutMs time out ms
* @return OkHttpClient
*/
public static OkHttpClient getClient(long timeOutMs) {
OkHttpClient.Builder mBuilder = new OkHttpClient.Builder();
mBuilder.sslSocketFactory(createSSLSocketFactory());
mBuilder.hostnameVerifier(new TrustAllHostnameVerifier());
mBuilder.readTimeout(timeOutMs, TimeUnit.MILLISECONDS);
mBuilder.connectTimeout(timeOutMs, TimeUnit.MILLISECONDS);
mBuilder.writeTimeout(timeOutMs, TimeUnit.MILLISECONDS);
OkHttpClient client = mBuilder.build();
return client;
}
3.修改后代码
/**
* get okhttpclient
*
* @param timeOutMs time out ms
* @return OkHttpClient
*/
public static OkHttpClient getClient(long timeOutMs) {
try {
// Create a trust manager that does not validate certificate chains
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
// okhttp3.OkHttpClient.Builder builder = new OkHttpClient.Builder();
// builder.sslSocketFactory(sslSocketFactory);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.sslSocketFactory(sslSocketFactory, new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
});
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
OkHttpClient okHttpClient = builder.build();
builder.readTimeout(timeOutMs, TimeUnit.MILLISECONDS);
builder.connectTimeout(timeOutMs, TimeUnit.MILLISECONDS);
builder.writeTimeout(timeOutMs, TimeUnit.MILLISECONDS);
return okHttpClient;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (KeyManagementException e) {
throw new RuntimeException(e);
}
}
4、到这里基本结束,只是简单记录下,后续有其他同学遇到这个坑这样解决就行。
感谢
runtime error - Vimeo Networking Library Crash for Android 10 platform (api29) - Stack Overflow
转载请注明出处Android 12 Unable to extract the trust manager on Android10Platform-CSDN博客,谢谢!