Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.net.ssl.SSLPeerUnverifiedException: No peer certificate while I am working with google place api in android [duplicate]

I am facing an javax.net.ssl.SSLPeerUnverifiedException: No peer certificate exception while working with google place api in android emulator.Below is my url:

https://maps.googleapis.com/maps/api/place/search/json?&location=28.632808,77.218276&radius=1000&sensor=false&key=AIzaSyC3y3fOMTqTkCjNNETQTCKustG7BRk_eVc

when i simply , paste above url in browser address bar , it simple gives Json string.So , i think there is no need of any certificate authentication.

After doing so many googling and devoted 2-3 days, i've used SSLSocketFacory class of org.apache.http.conn.ssl.SSLSocketFactory package to avoid peer certificate error.

below is my code :

public static HttpClient wrapClient(HttpClient base) {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            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;
                }
            };
            X509HostnameVerifier verifier = new X509HostnameVerifier() {

                public void verify(String string, SSLSocket ssls) throws IOException {
                }

                public void verify(String string, X509Certificate xc) throws SSLException {
                }

                public void verify(String string, String[] strings, String[] strings1) throws SSLException {
                }

                public boolean verify(String string, SSLSession ssls) {
                    return true;
                }
            };
            ctx.init(null, new TrustManager[]{tm}, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
            ssf.setHostnameVerifier(verifier);
            ClientConnectionManager ccm = base.getConnectionManager();
            SchemeRegistry sr = ccm.getSchemeRegistry();
            sr.register(new Scheme("https", ssf, 443));
            return new DefaultHttpClient(ccm, base.getParams());
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

I am using android api 16 , and when I used SSLSocketFactory(SSLSocketContext) constructor,eclipse gives compliation error , so i've search alot and download jar having SSLSocketFactory(SSLSocketContext) ,which is "httpclient-4.1.1.jar" file.And i also set order of that jar in project build path , have set highest priority.Now I am getting following warning:

VFY:unable to resolve direct method 4253:Lorg/apache/http/conn/ssl/SSLSocketFactory.<init>

And following long trace of error caused by

java.lang.NoSuchMethodError : org.apace.http.conn.ssl.SSLSocketFactory.<init>

Please , help me I don't know ,where I am wrong.

like image 896
ved Avatar asked Nov 21 '12 02:11

ved


1 Answers

I too had this problem, and searched lot of things in Internet, but I solved by a simple step

I don't know the exact Cause, But I solved it by Restarting Emulator or create a NEW Emulator, and Run the project in that new Emulator

I won't say this is the exact answer but Try it before modifying your coding with Complex codes

like image 144
Mani Avatar answered Nov 15 '22 00:11

Mani