1、在android10中sslSocketFactory过期解决办法
//信用管理器
private X509TrustManager getX509TrustManager() throws Exception {
X509TrustManager trustManager = new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] chain,
String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[0];
}
};
return trustManager;
}
//信用管理器
X509TrustManager trustManager = getX509TrustManager();
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{trustManager}, new java.security.SecureRandom());
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
//okhttp
OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
.writeTimeout(BaseConfig.WRITE_TIME, TimeUnit.SECONDS) .readTimeout(BaseConfig.READ_TIME, TimeUnit.SECONDS)
.connectTimeout(BaseConfig.CONNECT_TIME,TimeUnit.SECONDS)
.addInterceptor(new ApiInterceptor())
.sslSocketFactory(sslSocketFactory,trustManager)
.hostnameVerifier(org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
.build();
//retrofit
Retrofit mRetrofit = new Retrofit.Builder()
.baseUrl(BaseConfig.schoolUrl)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())//使用rxjava
.addConverterFactory(GsonConverterFactory.create())//使用Gson
.client(mOkHttpClient)
.build();