1

As the title says, I have code inside "doInBackground", in AsyncTask. The code reads text from Internet; if something goes wrong (e.g., connexion not available), though, it should signal this situation. Volatile variables are used (and are working) to flag status to another thread, running a Timer.

Now, the problem: at the call "urlConnection.connect() " the function ends (seen in debugger). App reports the string "Fase 3", but variables as "lendoInternet" or "contador" (the volatile ones) are left untouched - no exception caught (!). On the other hand, if I use the string "abracadabra" as (a bad) URL, then an exception is thrown.

My theory: the call "urlConnection.connect();" sends AsyncTask to Jupiter; I mean, it crashes so hard that even exception handling is not honoured (but: the Timer thread goes on updating the UI). Yes, stupid theory, but I need some light here, please.

This thing runs in the emulator, launched from Android Studio. By the way, at the finally block, the string in portuguese means "Hey Java, is your mother doing fine?", to show all my tenderness for this lovely programming language (replace everything with your preferred German curses).

Thanks guys.

        lendoInternet = true;
        erroInternet  = false;

        try {
            URL url = new URL("https://www-eng-x.llnl.gov/documents/a_document.txt");   result = "-Fase-2-";
            //URL url = new URL("abracadabra");   result = "-Fase-2-";
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); result = "-Fase-3-";
            urlConnection.connect();                                                    result = "-Fase-4-";
            InputStream is = new BufferedInputStream(urlConnection.getInputStream());   result = "-Fase-5-";
            BufferedReader r = new BufferedReader(new InputStreamReader(is));           result = "-Fase-6-";

            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line).append('\n');
            }
            result = total.toString();
        } catch (IOException t) {
            contador = 1;
            lendoInternet = false;
            erroInternet = true;
        } catch (Throwable t) {
            contador = 1;
            lendoInternet = false;
            erroInternet = true;
        }
        finally {
            result = "Sua maezinha vai bem, Java?";
        }
A Koscianski
  • 115
  • 1
  • 5
  • Debug your code where exactly is exception thrown . Point out the line if its inside `try` block which i don't think possible . – ADM Mar 16 '18 at 13:49
  • Try to put some log on the `catch` to see if it goes through it. Could you please post the logcat ? – Maxouille Mar 16 '18 at 13:49

2 Answers2

0

I think your code after urlConnection.connect() might be optimized out, as you assign a defined value to your variable result in your finally block.

Vincz777
  • 521
  • 5
  • 12
0

It turns out, the problem was waiting for the exception. I'll explain.

The code was frozen while trying to connect (computer is behind a proxy); by setting a short timeout, e.g.:

            urlConnection.setConnectTimeout(3000);

and waiting for it, voilà, the code worked as expected.

Now, what's the default timeout? The tests executed here for 10 seconds, with no luck; according to this the default timeout might be infinite.

Thanks for the comments and ideas.

A Koscianski
  • 115
  • 1
  • 5