clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on JDK 9+
at okhttp3.internal.platform.Jdk9Platform.trustManager(Jdk9Platform.kt:61)
at okhttp3.OkHttpClient$Builder.sslSocketFactory(OkHttpClient.kt:751)
at
1.升版本4.9.3以上
2、加个函数获取X509TrustManager
public static X509TrustManager getX509TrustManager() {X509TrustManager trustManager = null;try {TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());trustManagerFactory.init((KeyStore) null);TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();if (trustManagers.length != 1 || !(trustManagers[0] instanceof X509TrustManager)) {throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));}trustManager = (X509TrustManager) trustManagers[0];} catch (Exception e) {e.printStackTrace();}return trustManager;}
3.创建的时候加上上面的X509TrustManager
private static synchronized OkHttpClient getInstance() {okHttpClient = new OkHttpClient().newBuilder().connectTimeout(15, TimeUnit.SECONDS).readTimeout(15, TimeUnit.SECONDS).writeTimeout(15, TimeUnit.SECONDS).sslSocketFactory(new SSLSocketClient().getSSLSocketFactory(),SSLSocketClient.getX509TrustManager())//配置.hostnameVerifier(new SSLSocketClient().getHostnameVerifier())//配置.build();if (okHttpClient == null) {okHttpClient = new OkHttpClient();}return okHttpClient;}
重点是下面这句
.sslSocketFactory(new SSLSocketClient().getSSLSocketFactory(),SSLSocketClient.getX509TrustManager())
参考链接:使用okhttp3,提示【clientBuilder.sslSocketFactory(SSLSocketFactory) not supported on JDK 9+】