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?";
}