0
I have an application that queries a URL through the following method within a AsyncTask
:
try {
HttpURLConnection http = getHttpUrlConnection("http://www.sitedoservidor.com.br");
if (http != null) {
http.setRequestProperty("Cookie", "flag=0");
http.setInstanceFollowRedirects(true);
http.setConnectTimeout(10000);
http.setReadTimeout(10000);
http.connect();
Map<String, List<String>> reqMap = http.getHeaderFields();
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, List<String>> e : reqMap.entrySet()) {
for (String i : e.getValue()) {
if (e.getKey() != null && e.getKey().startsWith("Set-Cookie") && i.contains("SESSION")) {
sessionId = i.split(";")[0].trim();
}
}
}
http.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
As soon as the method is called, ProgressDialog
until the method returns the desired result.
To let the user know that the function is still working, parametrized progress.setCanceledOnTouchOutside(false)
to close only after the return of the expected method, without the user closing the app or get out of Activity
by accident.
The problem is that eventually, if the server is down, the application freezes in the http.connect()
and practically brakes the application, because the ProgressDialog
continues to function.
How can I treat this error so that when the server is down, the AsyncTask
stop, close the ProgressDialog
and thus warn the user that it was not possible to complete the action or that the URL is out of order?
When you make that mistake, you fall into
catch (IOException ex) {
?– Leonardo Dias
No. It just freezes. I would like at least one method to test the connection to the URL and that after a certain time, no server response, it went to 'catch (Ioexception ex)'
– Rogério Eduard Schaefer
I added a code that can help you, take a look at the answer
– Leonardo Dias