我面临一个 javax.net.ssl.SSLPeerUnverifiedException: No peer certificate exception while using 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字符串。所以,我认为不需要任何证书认证。
在做了这么多谷歌搜索并投入了 2-3 天之后,我使用了 org.apache.http.conn.ssl.SSLSocketFactory 包的 SSLSocketFacory 类来避免对等证书错误。
下面是我的代码:
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给出了编译错误,所以我搜索了很多并下载了具有 SSLSocketFactory(SSLSocketContext) 的 jar,即“httpclient-4.1.1.jar”文件。而且我还在项目构建路径中设置了该 jar 的顺序,设置了最高优先级。现在我收到以下警告:
VFY:unable to resolve direct method 4253:Lorg/apache/http/conn/ssl/SSLSocketFactory.<init>
并遵循由
java.lang.NoSuchMethodError : org.apace.http.conn.ssl.SSLSocketFactory.<init>
请帮助我,我不知道,我错在哪里。