Missing connection to server via HttpURLConnection

0

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 ?

    
asked by anonymous 25.05.2017 / 22:14

1 answer

1

Roger, you can do it this way:

Add another catch, which will be responsible for observing the TimeOut, if it pops the time without response from the server, it will fall into it, then just remove ProgressDialog

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 (SocketTimeoutException e) {
        //remove o loader aqui

        } catch (IOException ex) {
           ex.printStackTrace();
        }

SocketTimeoutException should come before the IOException

    
25.05.2017 / 22:41