This problem is not common, but I encountered it. It is not easy to search on the Internet. It is basically onFailure:javax.net.ssl.SSLHandshakeException, so I did not add this in front of my title
Reason:You can see if the system time on your device is wrong ←..←
Solution:
1. Change the time.
2. Check the local time of the mobile phone online p>
public class SSLCertificate {
public static SSLSocketFactory SSLSocketFactorygetSSLSocketFactory(){
final TrustManager[] trustAllCerts =new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
};
SSLContext sslContext =null;
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new< span style="color: #000000;"> java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
return sslSocketFactory;
}
}
Check OkHttpClient time
OkHttpClient.Builder builder = new OkHttpClient.Builder() .sslSocketFactory(SSLCertificate.SSLSocketFactorygetSSLSocketFactory()) .hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } });
OK, try it
public class SSLCertificate {
public static SSLSocketFactory SSLSocketFactorygetSSLSocketFactory(){
final TrustManager[] trustAllCerts =new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}
};
SSLContext sslContext =null;
try {
sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new< span style="color: #000000;"> java.security.SecureRandom());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
return sslSocketFactory;
}
}
OkHttpClient.Builder builder = new OkHttpClient.Builder() .sslSocketFactory(SSLCertificate.SSLSocketFactorygetSSLSocketFactory()) .hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } });