I am trying to check server url is reachable or not in android.
url which i want to check is like this: https://mydomain.se:8081/
and also reachable from internet. But getting a problem as sometime below method return true and some time false.
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;
}
is there any other alternative to check the server from android over mobile data?