0

I write an android app, and it works well before, however,recently, I found it is not working. My app parsing json code from the link below: http://opendata.epa.gov.tw/ws/Data/RainTenMin/?%24format=json

I found that the reason why my app is not working is that the response code is 302,So I google on the internet, and use getHeaderField("Location") in my code and I found that the redirected url is https://opendata.epa.gov.tw/ws/Data/RainTenMin/?%24format=json

So I changed my parsing url to the link with https, however, this time, HttpURLConnection throws IOEexception, how can i fix it, thank you for your help.

Below is my code throwing exception:

private static String httpRequest(URL rurl) throws IOException{
        String response = "";

        if (rurl == null) {
            return response;
        }
        HttpURLConnection connection = null;
        InputStream inputStream = null;
        try {
            connection = (HttpURLConnection) rurl.openConnection();
            connection.setConnectTimeout(20000);
            connection.setRequestMethod("GET");
            connection.setReadTimeout(25000);
            connection.connect();

            //200 means correctly connected
            int responseCode = connection.getResponseCode();

            if (responseCode ==200) {
                inputStream = connection.getInputStream();
                response = readStream(inputStream);
            }
            else {

                MainActivity.connect=false;
                Log.i(logtag, "Error!!  Response code is " + responseCode);
            }
        }
        catch (IOException e) {
            MainActivity.connect=false;
            Log.e(logtag, "bad internet!!");}
        finally {
            if (inputStream != null) {
                inputStream.close();
            }
            if (connection != null) {
                connection.disconnect();
            }
        }
        return response;
    } 

below is the logcat of the error:

E/query: bad internet!!

1 Answers1

0

What you're getting is a javax.net.ssl.SSLHandshakeException.

You can find some helpful information about how to fix this problem here: How to solve javax.net.ssl.SSLHandshakeException Error?

Excerpt:

TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {

                    public java.security.cert.X509Certificate[] getAcceptedIssuers()
                    {
                        return null;
                    }
                    public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType)
                    {
                        //No need to implement.
                    }
                    public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType)
                    {
                        //No need to implement.
                    }
                }
        };

        // Install the all-trusting trust manager
        try 
        {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } 
        catch (Exception e) 
        {
            System.out.println(e);
        }
anomeric
  • 688
  • 5
  • 17