title: How to check url is reachable or not
body: <p>I am trying to check server url is reachable or not in android.
  url which i want to check is like this: <code>https://mydomain.se:8081/</code> and also reachable from internet. But getting a problem as sometime below method return true and some time false.  </p>
  
  <pre><code>  public boolean isBackendAccessAble(String serverUrl) {
      String host = serverUrl.substring(serverUrl.indexOf("://") + 3, serverUrl.lastIndexOf(":"));
      int port = Integer.parseInt(serverUrl.substring(serverUrl.lastIndexOf(":") + 1, serverUrl.length() - 1));
      boolean isReachable = false;
      try {
          SocketAddress sockAddress = new InetSocketAddress(host, port);
          Socket sock = new Socket();
          int timeoutMs = 2000; // 2 seconds
          sock.connect(sockAddress, timeoutMs);
          isReachable = true;
      } catch (Exception e) {
          Log.w(TAG, "Unable to check serverUrl:" +   serverUrl);
      }
      return isReachable;
  }
  </code></pre>
  
  <p>is there any other alternative to check the server from android over mobile data?</p>