Missing server connection via Httpurlconnection

Asked

Viewed 207 times

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) {?

  • 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)'

  • I added a code that can help you, take a look at the answer

1 answer

1

Roger, you can do it this way:

Add one more catch, which will be responsible for observing the Timeout, if it runs out of time without response from the server, will fall in it, then just remove the 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();
        }

The Sockettimeoutexception should come before the Ioexception

  • Thank you very much Leonardo for the suggestion. At the moment the URL is working perfectly and so I can not test. As soon as I have a result, I will post it here. Thanks again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.