Why don't you move the check for connectivity inside of the AsyncTask then? As opposed to checking before and then running the task to find out you don't have it, why not check inside the AsyncTask DIRECTLY before you actually do anything with the internet, inside of doInBackground
.
EDIT: I think I may have figured out what's wrong. You set Document doc = null
and then in your try statement, you set doc
to be whatever you get from the internet. However, if that doesn't work, your doc
remains null AND you keep going, assuming doc
has something in it. Make your doInBackground
look like this:
protected String doInBackground(String... arguments) {
// extract arguments
String newsurl = arguments[0];
//
Document doc = null;
try {
doc = Jsoup.connect(newsurl).get();
Elements myElements = doc.getElementsByClass("news_list");
string1 = myElements.toString();
Log.i("ELEMENTS HTML", string1);
} catch (IOException e) {
e.printStackTrace();
string1 = null;
} catch (NullPointerException e) {
e.printStackTrace();
string1 = null;
}
return string1;
}
And in your onPostExecute
, check to see if result
is null and do something accordingly.