Sunday 26 March 2017

how to stop asynctask when internet is connected but no internet access in android

String url = "http://xyzcloud.com/xyz/xyz.php";


this the url am trying to get the data from cloud,if internet is connected then async task will execute
i wrote this method for checking wether internet is connected or not, here in my mobile wifi is connetecd when am trying to execute it is giving exception



public boolean checkOnlineState()
{
boolean val = false;
ConnectivityManager CManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo NInfo = CManager.getActiveNetworkInfo();
if (NInfo != null && NInfo.isConnectedOrConnecting()) {
try {
if (InetAddress.getByName(url).isReachable(10000))
{
// host reachable
val=true;
Log.e("checkOnlineState", "" + val);
}
else

{
// host not reachable
val = false;
Log.e("checkOnlineState", "" + val);
}
} catch (IOException e) {
e.printStackTrace();
}
}


return val;
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...