public static boolean isHostReachable(String hostUrl){
  if (!hostUrl.contains("://"))   hostUrl="http://" + hostUrl;
  boolean isReachable=false;
  try {
    final URL url=new URL(hostUrl);
    final HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
    httpURLConnection.setRequestProperty("User-Agent","Android Application");
    httpURLConnection.setRequestProperty("Connection","close");
    httpURLConnection.setConnectTimeout(TIME_OUT);
    httpURLConnection.connect();
    final int responseCode=httpURLConnection.getResponseCode();
    isReachable=responseCode > 0;
  }
 catch (  MalformedURLException e) {
    Log.e(e);
  }
catch (  IOException e) {
    Log.e(e);
  }
  return isReachable;
}

public static boolean isHostReachable ( String hostUrl ) { if ( ! hostUrl . contains ( "://" ) ) hostUrl = "http://" + hostUrl ; boolean isReachable = false ; try { final URL url = new URL ( hostUrl ) ; final HttpURLConnection httpURLConnection = ( HttpURLConnection ) url . openConnection ( ) ; httpURLConnection . setRequestProperty ( "User-Agent" , "Android Application" ) ; httpURLConnection . setRequestProperty ( "Connection" , "close" ) ; httpURLConnection . setConnectTimeout ( TIME_OUT ) ; httpURLConnection . connect ( ) ; final int responseCode = httpURLConnection . getResponseCode ( ) ; isReachable = responseCode > 0 ; } catch ( MalformedURLException e ) { Log . e ( e ) ; } catch ( IOException e ) { Log . e ( e ) ; } return isReachable ; }
checks host by connection to using httpurlconnection . method should not be run on a main / uithread otherwise exception will be thrown .
