Android : how to stop a running async task?

on Friday, December 5, 2014


I am running a async task in android to ping t a particular URL but the problem is when the url is not valid or not reachable an exception occurs as sockettimeoutexception so on that exception i want to stop the running task. iagve tried using Cancel() method but that causes App crash.


I am using the following code.



private class UrlDataProvider3 extends AsyncTask<String, Void, String>
{

String ret="";
int checkStatus;
Boolean exception=false;
@Override
protected String doInBackground(String... url)
{

HttpURLConnection con = null;

try
{

Log.i("RAE", "urlData"+url[0]);
HttpURLConnection.setFollowRedirects(true);
con = (HttpURLConnection) new URL(url[0]).openConnection();
con.setRequestMethod("POST");
con.setConnectTimeout(20000);




}


catch (IOException e)
{


if(e.toString().contains("java.net.SocketTimeoutException:"))
{



return null;

}



}



return ret;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
Log.i("RAE"," Asyc finished");



}

0 comments:

Post a Comment