public static boolean isHostReachable(final String hostUrl){
  boolean doesHostRespond=false;
  try {
    final URL url=new URL(hostUrl.contains("://") ? hostUrl : "http://" + hostUrl);
    setTrustAnySSLCertificateMode();
    final HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
    httpURLConnection.setRequestProperty("Connection","close");
    httpURLConnection.setConnectTimeout(TIME_OUT);
    httpURLConnection.connect();
    final int responseCode=httpURLConnection.getResponseCode();
    doesHostRespond=responseCode < 400;
  }
 catch (  MalformedURLException e) {
    e.printStackTrace();
  }
catch (  SocketTimeoutException e) {
    e.printStackTrace();
  }
catch (  IOException e) {
    e.printStackTrace();
  }
catch (  SecurityException e) {
    e.printStackTrace();
  }
catch (  NoSuchAlgorithmException e) {
    e.printStackTrace();
  }
catch (  KeyManagementException e) {
    e.printStackTrace();
  }
catch (  OutOfMemoryError e) {
    e.printStackTrace();
  }
  return doesHostRespond;
}

public static boolean isHostReachable ( final String hostUrl ) { boolean doesHostRespond = false ; try { final URL url = new URL ( hostUrl . contains ( "://" ) ? hostUrl : "http://" + hostUrl ) ; setTrustAnySSLCertificateMode ( ) ; final HttpURLConnection httpURLConnection = ( HttpURLConnection ) url . openConnection ( ) ; httpURLConnection . setRequestProperty ( "Connection" , "close" ) ; httpURLConnection . setConnectTimeout ( TIME_OUT ) ; httpURLConnection . connect ( ) ; final int responseCode = httpURLConnection . getResponseCode ( ) ; doesHostRespond = responseCode < 400 ; } catch ( MalformedURLException e ) { e . printStackTrace ( ) ; } catch ( SocketTimeoutException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( SecurityException e ) { e . printStackTrace ( ) ; } catch ( NoSuchAlgorithmException e ) { e . printStackTrace ( ) ; } catch ( KeyManagementException e ) { e . printStackTrace ( ) ; } catch ( OutOfMemoryError e ) { e . printStackTrace ( ) ; } return doesHostRespond ; }
checks host by connection to using httpurlconnection . method should not be run on a main / uithread otherwise exception will be thrown .
