javax.net.ssl.SSLProtocolException 调用 HttpUrlConnection.connect()
javax.net.ssl.SSLProtocolException while calling HttpUrlConnection.connect()
我正在尝试使用 GoogleMap 的 api for Android 在我的应用程序中创建地点搜索功能。我想在用户键入地点名称时填充 AutoComplete TextView。但是我在调用 urlConnection.connect();
语句时遇到了这个异常:
javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb911cd50: Failure in SSL library, usually a protocol error
error:1407743E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert inappropriate fallback (external/openssl/ssl/s23_clnt.c:765 0xae688edd:0x00000000)
以下是我使用的代码:
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e) {
Log.d("Exception while downloading url", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
private String getPlacesUrl(String qry) {
try {
qry = "input=" + URLEncoder.encode(qry, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// Sensor enabled
String sensor = "sensor=false";
// place type to be searched
String types = "types=geocode";
// Building the parameters to the web service
String parameters = qry + "&" + types + "&" + sensor + "&" + mKey;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/autocomplete/" + output + "?" + parameters;
return url;
}
我已经在 google 控制台中注册了我的应用程序包和系统的 SHA1。我在调用 getPlaceUrl()
方法后得到的 url 是这种类型的:
请帮我解决这个问题。
我遇到了几乎相同的问题。
我的logcat如下:
javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException:
SSL 握手中止:ssl=0x60b486b8:SSL 库失败,通常是协议错误
SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 警告不适当的回退 (external/openssl/ssl/s23_clnt.c:744 0x5ea50d74:0x00000000)
最后,我找到了一个解决方案,就是将 httpURLConnection.setHostnameVerifier HostnameVerifier verify return 的值从 false 修改为 true,但我不知道根本原因。
有没有人可以提供提示?
httpURLConnection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// TODO Auto-generated method stub
// return false;
return true;
}
});
我正在尝试使用 GoogleMap 的 api for Android 在我的应用程序中创建地点搜索功能。我想在用户键入地点名称时填充 AutoComplete TextView。但是我在调用 urlConnection.connect();
语句时遇到了这个异常:
javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb911cd50: Failure in SSL library, usually a protocol error
error:1407743E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert inappropriate fallback (external/openssl/ssl/s23_clnt.c:765 0xae688edd:0x00000000)
以下是我使用的代码:
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
HttpURLConnection urlConnection = null;
try {
URL url = new URL(strUrl);
// Creating an http connection to communicate with url
urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e) {
Log.d("Exception while downloading url", e.toString());
} finally {
iStream.close();
urlConnection.disconnect();
}
return data;
}
private String getPlacesUrl(String qry) {
try {
qry = "input=" + URLEncoder.encode(qry, "utf-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// Sensor enabled
String sensor = "sensor=false";
// place type to be searched
String types = "types=geocode";
// Building the parameters to the web service
String parameters = qry + "&" + types + "&" + sensor + "&" + mKey;
// Output format
String output = "json";
// Building the url to the web service
String url = "https://maps.googleapis.com/maps/api/place/autocomplete/" + output + "?" + parameters;
return url;
}
我已经在 google 控制台中注册了我的应用程序包和系统的 SHA1。我在调用 getPlaceUrl()
方法后得到的 url 是这种类型的:
请帮我解决这个问题。
我遇到了几乎相同的问题。 我的logcat如下:
javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL 握手中止:ssl=0x60b486b8:SSL 库失败,通常是协议错误 SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 警告不适当的回退 (external/openssl/ssl/s23_clnt.c:744 0x5ea50d74:0x00000000)
最后,我找到了一个解决方案,就是将 httpURLConnection.setHostnameVerifier HostnameVerifier verify return 的值从 false 修改为 true,但我不知道根本原因。 有没有人可以提供提示?
httpURLConnection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
// TODO Auto-generated method stub
// return false;
return true;
}
});