How to chake if server connection is exist? : In my app I have asyncTask classes, that connect to the local/remote server to get some data, I want to check the server connection before the asyncTask is run,
I got this function:
 

      public static boolean checkServerAvailable(String hostURL)
	     {
		      HttpURLConnection connection = null;
		      try {
		          URL u = new URL(hostURL);
		          connection = (HttpURLConnection) u.openConnection();
		          connection.setConnectTimeout(5000);
		          connection.setRequestMethod("HEAD");
		          int code = connection.getResponseCode();
		          System.out.println("" + code);
		          return true;
		          // You can determine on HTTP return code received. 200 is success.
		      } catch (MalformedURLException e) {
		          // TODO Auto-generated catch block
		          e.printStackTrace();
		          return false;
		      } catch (IOException e) {
		          // TODO Auto-generated catch block
		          e.printStackTrace();
		          return false;
		      } finally {
		          if (connection != null) {
		              connection.disconnect();
		          }
		      }
	     }

this function using timeout to connect to server, and if the timeout is finish it's mean that there is no server, the problem is that I'm run this code and it's returning  "there is no server" if the server is exist too,
I'm tried to set a big timeout like 5000ms but it's stop the UI very long time, and somtiems return "there is no server" when the server is exist too.

what can I do?
Thank you!