Rahul Giradkar
Rahul Giradkar

Reputation: 1818

Facing error while call https Api through volley

I have 3 sever setup with same code.

  1. Server 1 without SSL : Its working fine.
  2. Server 2 with SSL on production sever (Authorize certificate) : Its working fine.
  3. Server 3 with SSL on Test sever (Authorize certificate same certification as Server 2) : But It is not working. It throws an exception as follow.

Error :

 onErrorResponse: com.android.volley.NoConnectionError: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
                                in.chsone.member.exceptions.UnhandledException: error.networkResponse is null.
                                    at in.chsone.member.network.NetworkResponseHandler.onErrorResponse(NetworkResponseHandler.java:181)
                                    at com.android.volley.Request.deliverError(Request.java:598)
                                    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
                                    at android.os.Handler.handleCallback(Handler.java:739)
                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                    at android.os.Looper.loop(Looper.java:148)
                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                    at java.lang.reflect.Method.invoke(Native Method)
                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Upvotes: 1

Views: 109

Answers (1)

Darshan Kachhadiya
Darshan Kachhadiya

Reputation: 337

public class HttpsTrustManager implements X509TrustManager {

private static TrustManager[] trustManagers;
private static final X509Certificate[] _AcceptedIssuers = new X509Certificate[]{};

@Override
public void checkClientTrusted(
        java.security.cert.X509Certificate[] x509Certificates, String s)
        throws java.security.cert.CertificateException {

}

@Override
public void checkServerTrusted(
        java.security.cert.X509Certificate[] x509Certificates, String s)
        throws java.security.cert.CertificateException {

}

public boolean isClientTrusted(X509Certificate[] chain) {
    return true;
}

public boolean isServerTrusted(X509Certificate[] chain) {
    return true;
}

@Override
public X509Certificate[] getAcceptedIssuers() {
    return _AcceptedIssuers;
}

public static void allowAllSSL() {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }

    });

    SSLContext context = null;
    if (trustManagers == null) {
        trustManagers = new TrustManager[]{new HttpsTrustManager()};
    }

    try {
        context = SSLContext.getInstance("TLS");
        context.init(null, trustManagers, new SecureRandom());
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    }

    HttpsURLConnection.setDefaultSSLSocketFactory(context
            .getSocketFactory());
}

}

Call below line before call api in volly

HttpsTrustManager.allowAllSSL();

Upvotes: 2

Related Questions