gpt4 book ai didi

java - 尝试在 Android 上连接到 https ://maps. googleapis.com 时出现 SSLHandshakeException

转载 作者:行者123 更新时间:2023-12-02 04:05:01 27 4
gpt4 key购买 nike

我想从 google place 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

最佳答案

我没有使用“HttpsURLConnection”,而是使用 netcipher 。这是使用:在您的 Build.gradle 的依赖项 block 中,

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,然后将输入流读入字符串:

    if (statusCode == 200) {
InputStream inputStream = urlConnection.getInputStream();
}

SSLHandshakeException 不会出现,并且代码可以工作。PS:抱歉回复晚了!

关于java - 尝试在 Android 上连接到 https ://maps. googleapis.com 时出现 SSLHandshakeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36215850/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com