2

please check following code

 public boolean isOnline() {

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
 int networkType = ConnectivityManager.TYPE_WIFI; 
return cm.requestRouteToHost(networkType, lookupHost("http://www.ati.ag"));
 //return cm.getActiveNetworkInfo().isConnectedOrConnecting();

}

  public static int lookupHost(String hostname) {
InetAddress inetAddress;
try {
    inetAddress = InetAddress.getByName(hostname);
} catch (UnknownHostException e) {
    return -1;
}
byte[] addrBytes;
int addr;
addrBytes = inetAddress.getAddress();
addr = ((addrBytes[3] & 0xff) << 24)
        | ((addrBytes[2] & 0xff) << 16)
        | ((addrBytes[1] & 0xff) << 8)
        |  (addrBytes[0] & 0xff);
System.out.println(addr);
return addr;

}

isOnline always returns false ,please help how does it work? Or please tell me about any other way of checking server connectivity Basic purpose is to check that , internet is available on local wifi or not

Ali
  • 9,900
  • 10
  • 53
  • 83

2 Answers2

0

You need to specify a host name to InetAddress.getByName, not a URL, so this line:

return cm.requestRouteToHost(networkType, lookupHost("http://www.ati.ag"));

Should be:

return cm.requestRouteToHost(networkType, lookupHost("www.ati.ag"));

Probably this will work better.

gimix
  • 736
  • 6
  • 21
  • Now I realize there is an old unanswered question about that. One possible explanation is that it is not implemented. See: http://stackoverflow.com/a/2680765/972850 – gimix Dec 16 '11 at 14:42
0

As an alternative to your approach, if your purpose is only to check if Wifi is available you can try this. This worked for me:

ConnectivityManager cm=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm==null)
    return false;
NetworkInfo networkInfo=cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(networkInfo==null)
    return false;
return networkInfo.isConnected();
gimix
  • 736
  • 6
  • 21
  • The question is talking about Internet on Wifi, have a look at the problem here http://stackoverflow.com/questions/6493517/android-detect-if-device-has-internet-connection – Gaurav Agarwal May 28 '12 at 20:34