SSL handshake aborted, when i am calling API in Jellybean

311 Views Asked by At

I am working on a below 21 api level project and when i am calling an API then the error showing on jellybean that

SSL handshake aborted

I try many methods but only one method is working on kitkat like:

try {
                SSLContext.getInstance("TLSv1");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
            try {
                ProviderInstaller.installIfNeeded(context.getApplicationContext());
            } catch (GooglePlayServicesRepairableException e) {
                e.printStackTrace();
            } catch (GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }

but this method is not working on Jellybean. There are another error also showing that:

W/GooglePlayServicesUtil: Google Play services out of date. Requires 13400000 but found 9256030

Please suggest me best solution.

2

There are 2 best solutions below

4
gpuser On

Try to change this SSLContext.getInstance("TLS")

2
gpuser On

you can try this way

SSLContext mySSLContext = SSLContext.getInstance("TLS" );
 mySSLContext.init( null, trustAllCerts, new SecureRandom());

 OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .hostnameVerifier( myHostnameVerifier )
                    .sslSocketFactory( mySSLContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
                    .build();


HostnameVerifier myHostnameVerifier = new HostnameVerifier() {
        @Override
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    };

    TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[]{};
        }

        public void checkClientTrusted(X509Certificate[] chain,
                                       String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain,
                                       String authType) throws CertificateException {
        }
    }};