0

My Android app is trying to connect to this URL:

  https://data.gov.in/api/datastore/resource.json?resource_id=e16c75b6-7ee6-4ade-8e1f-2cd3043ff4c9&api-key=APIKEY

This url give JSON Data. Since the protocol is https, the normal http call don't work. Every time I tries to make a call to this server, it says:

javax.net.ssl.SSLException: SSL handshake aborted: ssl=0x65f1e9f8: I/O error during system call, Connection reset by peer
 at com.android.org.conscrypt.NativeCrypto.SSL_do_handshake(Native Method)
 at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:406)
  at com.android.okhttp.Connection.upgradeToTls(Connection.java:146)

I've tried many codes but all seems to give the same error. How can I make a call to the URL above to get the JSON Data. I've no info of the server, only the URL is what I got (with an API KEY).

A working code to get the response as a string will be great.

manfcas
  • 1,933
  • 7
  • 28
  • 47
Basu
  • 763
  • 8
  • 27
  • Please check you can download a JSON from a valid hosted W/S as below and let me know. In addition, are you using an Android 6 OS? https://jsonplaceholder.typicode.com/posts – Ruchira Randana Dec 11 '16 at 17:49
  • Yes, when I open the url with a valid API key, I get the same response. And the app uses Ice-Cream Sandwhich - Nougat – Basu Dec 11 '16 at 17:57
  • using org.json library, [see this answer](http://stackoverflow.com/a/4308662/3682535) for `readFromUrl()` method in `main` method. – rupinderjeet Dec 11 '16 at 18:20
  • @rupinderjeet You sure this will get the content from https url and won't show SSL Handshake error? – Basu Dec 11 '16 at 18:32
  • I have not tried your URL, but i have tried many other https Urls to get data. They work as they should. – rupinderjeet Dec 11 '16 at 18:33
  • If you can provide API key, i can test for you. I have a Json function ready. – rupinderjeet Dec 11 '16 at 18:47
  • https://data.gov.in/api/datastore/resource.json?resource_id=e16c75b6-7ee6-4ade-8e1f-2cd3043ff4c9&api-key=5ece0d6285f0eb1e6df19f5c4b6f85e0 – Basu Dec 11 '16 at 18:52

1 Answers1

0

Your server data.gov.in seems to be configured only to use TLS 1.1. As you're using OKHTTP, set the device to use TLS 1.1.

E.g:

client.setConnectionSpecs(Collections.singletonList(spec));ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)  
    .tlsVersions(TlsVersion.TLS_1_1)
    .cipherSuites(
        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
        CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256,
        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
        CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
        CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
        CipherSuite.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
        CipherSuite.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
        CipherSuite.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
        CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA,
        CipherSuite.TLS_DHE_DSS_WITH_AES_128_CBC_SHA,
        CipherSuite.TLS_DHE_RSA_WITH_AES_256_CBC_SHA)
    .build();
client.setConnectionSpecs(Collections.singletonList(spec));

You can find how to configure OKHTTP as below https://github.com/square/okhttp/wiki/HTTPS

Ruchira Randana
  • 4,021
  • 1
  • 27
  • 24
  • I've tried those with Volley and Http Client. Trying with OKHTTP. Can you provide some more codes. Like what to do before and after this code. Really appreciate your help. – Basu Dec 11 '16 at 18:14
  • Hi Basu. From my understanding it seems like the website's SSL configuration only allows TLS 1.1. Can u please use another Android device running 4.4 upwards to confirm that your code works on that. If so, then we've identified the problem. – Ruchira Randana Dec 11 '16 at 18:18
  • I tried on Lolipop. But the code doesn't work. Still shows the same error. – Basu Dec 11 '16 at 18:20
  • Hi Ruchira. I made it working by implementing my own server between the app and the gov.in server. The app now connects with my app which fetch the data from gov.in and then returns back the response. My server uses curl to get the content. – Basu Dec 12 '16 at 06:53