Reputation: 383
I'm trying to get some info from a website (some text) in my app. The problem is that sometimes my method doesn't work. I have built this and use it for checking for internet connectivity:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
However, sometimes phones tell the app that tere is indeed an internet connection, when there is no actual stable one, and then the app crashes.
The actual refresh of the info is in an Async task and is executed like this, where crackerTask is my async task:
if (this.isNetworkAvailable()) {
crackerTask.execute(TESTSTRINGURL);}
else {
Toast.makeText(NewsAndAnnouncements.this,
"No web connectivity. Try again later.", Toast.LENGTH_LONG)
.show();
}
How can I avoid the crash of the program when my phone tells the app there is internet, while there isn't?
EDIT: here's the async task if that helps.
private class NewsAsyncTask extends AsyncTask<String, String, String> {
ProgressDialog dialog1;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog1 = ProgressDialog.show(NewsAndAnnouncements.this, "Loading",
"Fetching data from web...");
}
@Override
protected String doInBackground(String... arguments) {
// extract arguments
String newsurl = arguments[0];
//
Document doc = null;
try {
doc = Jsoup.connect(newsurl).get();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
Elements myElements = doc.getElementsByClass("news_list");
string1 = myElements.toString();
Log.i("ELEMENTS HTML", string1);
return string1;
}
@Override
protected void onPostExecute(String result) {
// super.onPostExecute(result);
FINALSTRING = result;
tv2.setText(FINALSTRING);
if (dialog1 != null) {
if (dialog1.isShowing()) {
dialog1.dismiss();
}
}
}
}
Upvotes: 0
Views: 975
Reputation: 389
method to check the internet connection
public boolean isConnectingToInternet()
{
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
and in the needed activity call like that
cd = new ConnectionDetector(getApplicationContext());
// Check if Internet present
if (!cd.isConnectingToInternet()) {
// Internet Connection is not present
alert.showAlertDialog(push.this,
"Internet Connection Error",
"Please connect to working Internet connection", false);
return;
}
Upvotes: 0
Reputation: 753
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.
Upvotes: 2