我想从google places api获取数据,但我收到了SSLHandshakeException。我能够在浏览器中运行相同的URL并获得结果。
但是在设备上测试,android 5.0.2会出错。我的应用程序的minSDK版本为16,TargetSDK版本为23,并在Android Studio 1.5上进行开发。 代码是:
HttpsURLConnection urlConnection = null;
Uri builtUri = Uri.parse(PLACE_BASE_URL).buildUpon()
.appendQueryParameter(LOCATION, location)
.appendQueryParameter(RADIUS, radius)
.appendQueryParameter(TYPE_PARAM, type)
.appendQueryParameter(SENSOR, Boolean.toString(true))
.appendQueryParameter(API_KEY, "AIzadfdajl_XXXXX-your_key")
.build();
URL url = new URL(builtUri.toString());
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
该行" urlConnection.connect()"给出javax.net.ssl.SSLHandshakeException错误。完整错误如下:
javax.net.ssl.SSLHandshakeException: Handshake failed
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:374)
at com.android.okhttp.Connection.upgradeToTls(Connection.java:201)
at com.android.okhttp.Connection.connect(Connection.java:155)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:106)
at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.connect(DelegatingHttpsURLConnection.java:89)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:25)
at info.placequest.android.GetPlaces.GetPlacesTask.doInBackground(GetPlacesTask.java:91)
at info.placequest.android.GetPlaces.GetPlacesTask.doInBackground(GetPlacesTask.java:26)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0x5585123b00: 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 0x7fa8990ca0:0x00000000)
at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:302)
... 16 more
答案 0 :(得分:0)
我使用netcipher而不是使用'HttpsURLConnection'。这里是用途: 在您的Build.gradle中,依赖项块,
implementation 'info.guardianproject.netcipher:netcipher:2.1.0'
然后在.java文件中访问URL,如下所示:
urlConnection = NetCipher.getHttpsURLConnection(requestUrl);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
接下来,您可以检查statusCode是否为200,然后将输入流读取为String:
if (statusCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
}
SSLHandshakeException将不会出现,并且代码可以正常工作。 PS:对不起,延迟答复!