javax.net.ssl.SSLPeerUnverifiedException:当我在 android 中使用 google place api 时没有对等证书 [英] javax.net.ssl.SSLPeerUnverifiedException: No peer certificate while I am working with google place api in android
问题描述
我正面临一个 javax.net.ssl.SSLPeerUnverifiedException:在 android 模拟器中使用 google place api 时没有对等证书异常.下面是我的网址:
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
当我简单地将上面的 url 粘贴到浏览器地址栏中时,它简单地给出了 Json 字符串.所以,我认为不需要任何证书认证.
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.
经过如此多的谷歌搜索并投入了 2-3 天,我使用了 org.apache.http.conn.ssl.SSLSocketFactory 包的 SSLSocketFacory 类来避免对等证书错误.
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.
下面是我的代码:
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;
}
}
我使用的是 android api 16 ,当我使用 SSLSocketFactory(SSLSocketContext) 构造函数时,eclipse 给出了编译错误,所以我搜索了很多并下载了 jarSSLSocketFactory(SSLSocketContext) ,这是httpclient-4.1.1.jar"文件.我还在项目构建路径中设置了那个jar的顺序,设置了最高优先级.现在我收到以下警告:
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>
以及以下由
java.lang.NoSuchMethodError : org.apace.http.conn.ssl.SSLSocketFactory.<init>
请帮帮我,我不知道我错在哪里.
Please , help me I don't know ,where I am wrong.
推荐答案
我也遇到了这个问题,在网上查了很多东西,但我一个简单的步骤就解决了
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
这篇关于javax.net.ssl.SSLPeerUnverifiedException:当我在 android 中使用 google place api 时没有对等证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!