Android : sockettimeoutexception causing app hang

on Friday, December 5, 2014


I am trying to ping the server in asynk task but the i am getting sockettimeout exception when i enter a invalid port number so that causes app hang. Post method of the asynk task is never called & app does not respond may i know why it happening. I am using the following code for this 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 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