title: how to stop a running async task?
body: <p>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 <strong>Cancel()</strong> method but that causes App crash.</p>
  
  <p>I am using the following code.</p>
  
  <pre><code>private class UrlDataProvider3 extends AsyncTask&lt;String, Void, String&gt;
  {
  
      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");
  
  
  
  }
  </code></pre>