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!!