Getting SSLHandshakeException when trying to connect to https://maps.googleapis.com on android

2

I want to get data from google places api, but I am getting the SSLHandshakeException. I am able to run the same URL in browser and getting the result.

But testing on device, android 5.0.2 gives error. My app's minSDK version is 16 and TargetSDK version is 23 and developing on Android Studio 1.5. The code is :

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();

The line "urlConnection.connect()" gives javax.net.ssl.SSLHandshakeException error. Full error is as follows:

    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
java
android
android-studio
google-maps-api-3
asked on Stack Overflow Mar 25, 2016 by userAbhi

1 Answer

0

Instead of using 'HttpsURLConnection' I used netcipher. Here is the use: In your Build.gradle, dependencies block,

implementation 'info.guardianproject.netcipher:netcipher:2.1.0' 

Then in the .java file access the URL as follows :

urlConnection = NetCipher.getHttpsURLConnection(requestUrl);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();

Next you can check if statusCode is 200,then read the input stream into a String:

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

The SSLHandshakeException will not come, and code works. PS: Sorry for the delayed answer!

answered on Stack Overflow Jul 12, 2019 by userAbhi

User contributions licensed under CC BY-SA 3.0