I have an application that queries a URL using 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, I call ProgressDialog
until the method returns the desired result.
In order for the user to know that the function is still working, you have set progress.setCanceledOnTouchOutside(false)
to close only after returning the expected method, without the user closing the app or quitting Activity
by accident.
The problem is that eventually, if the server is down, the application freezes in http.connect()
and almost crashes the application because ProgressDialog
is still running.
How can I handle this error so that when the server is down, AsyncTask
quits, quit ProgressDialog
and then warns the user that the action could not be completed or that the URL is down ?