com.android.org.bouncycastle.jce.exception.ExtCertPathValida

时间:2021-01-17 17:48:31

这个问题不常见,但是我遇到了,网上不好搜,基本是onFailure:javax.net.ssl.SSLHandshakeException,所以我标题没有前面加上这个

原因:你看下你设备上系统时间是不是不对←..←

解决:

1、时间改过来啊

2、联网校验手机本地时间

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 java.security.SecureRandom());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

        final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();

        return sslSocketFactory;
    }
}

对 OkHttpClient 时间校验

OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .sslSocketFactory(SSLCertificate.SSLSocketFactorygetSSLSocketFactory())
                .hostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String s, SSLSession sslSession) {
                        return true;
                    }
                });

 OK,你试试吧