java - how to stop a running async task? -


this question has answer here:

i running async task in android ping t particular url problem when url not valid or not reachable exception occurs sockettimeoutexception on exception want stop running task. iagve tried using cancel() method causes app crash.

i using 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");    } 

you this:

if((this.downloadnettask != null)        && (this.downloadnettask.getstatus() != asynctask.status.finished)){         this.downloadnettask.cancel(true); } 

where downloadnettask instance of async task. in async task implement following methods want when task cancelled.

protected void oncancelled(list<companynsequotedatavo> result)  protected void oncancelled() 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -