SSL Exception when using Volley

前端 未结 3 1147
孤城傲影
孤城傲影 2021-02-04 11:20

I\'m using Volley in Android to perform my app requests. Unfortunately, I\'m getting the following error:

com.android.volley.NoConnectionError: javax.net.ssl.SSL         


        
相关标签:
3条回答
  • 2021-02-04 12:01

    After our comments maybe this can help you:

    Your requestQueue:

    static {
        requestQueue = Volley.newRequestQueue(Application.getContext(), new HurlStack(null, ClientSSLSocketFactory.getSocketFactory()));
    }
    

    The ClientSSLSocketFactory:

    public class ClientSSLSocketFactory extends SSLCertificateSocketFactory {
        private SSLContext sslContext;
    
        public static SSLSocketFactory getSocketFactory(){
            try
            {
                X509TrustManager tm = new X509TrustManager() {
                    public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {}
    
                    public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {}
    
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                };
                sslContext = SSLContext.getInstance("TLS");
                sslContext.init(null, new TrustManager[] { tm }, null);
    
                SSLSocketFactory ssf = ClientSSLSocketFactory.getDefault(10000, new SSLSessionCache(Application.getInstance()));
    
                return ssf;
            } catch (Exception ex) {
                return null;
            }
        }
    
        @Override
        public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
            return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
        }
    
        @Override
        public Socket createSocket() throws IOException {
            return sslContext.getSocketFactory().createSocket();
        }
    }
    
    0 讨论(0)
  • 2021-02-04 12:01

    Add following import statments.

    import javax.net.ssl.TrustManager;
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.X509TrustManager;
    import java.security.cert.X509Certificate;
    import java.util.ResourceBundle;
    

    and add the following code before making network call.

        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
            public void checkClientTrusted(X509Certificate[] certs, String authType) {
            }
            public void checkServerTrusted(X509Certificate[] certs, String authType) {
            }
        } };
        SSLContext sc = null;
        try {
            sc = SSLContext.getInstance("SSL");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        try {
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    
    0 讨论(0)
  • 2021-02-04 12:12

    The only method that worked for me was this

    first put below method in your project Application class

    private void updateAndroidSecurityProvider() {
        try { 
            ProviderInstaller.installIfNeeded(this); 
        } catch (Exception e) {
            e.getMessage(); 
        } 
    }
    

    and then call it in onCreate method.

    I'm not faced any issue yet.

    reference link

    0 讨论(0)
提交回复
热议问题