java - sockettimeoutexception causing app hang -


i trying ping server in asynk task getting sockettimeout exception when enter invalid port number causes app hang. post method of asynk task never called & app not respond may know why happening. using following code purpose.

    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:"))             {                  log.i("hello","exception occurs");                  return null;             }            }     return ret;     }     @override     protected void onpostexecute(string result) {         // todo auto-generated method stub         super.onpostexecute(result);         log.i("rae"," asyc finished");  } 

you need catch sockettimeoutexception , return statement in catch block go onpostexecute

catch (sockettimeoutexception ste) {     return null; } catch (ioexception e) {        return null;  } 

and handle in onpostexecute

@override protected void onpostexecute(string result) {     super.onpostexecute(result);    log.i("rae"," asyc finished");    if (result !=null)    {        // code    }    else    {         // got exception    } 

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 -