Thursday, 2 July 2015

android reachability



Below code doesn't work, because:  "isReachable()" need root privileges.

http://stackoverflow.com/questions/2935325/android-debugging-inetaddress-isreachable/2935468#2935468

boolean reachable = false;

try {
    reachable = InetAddress.getByName("www.google.com").isReachable(2000);
} catch (UnknownHostException e) {
    // TODO Auto-generated catch block    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block    e.printStackTrace();
}

if(reachable){
    System.out.println("------========s=s=s=s=s=" +reachable);
} else{
    System.out.println("------========s=s=s=s=s=" +reachable);
}





use this code:



try{
    URL myUrl = new URL("http://www.google.com");
    URLConnection connection = myUrl.openConnection();
    connection.setConnectTimeout(5000);
    connection.connect();
} catch (Exception e) {
    // Handle your exceptions    e.printStackTrace();
    System.out.println("------========s=s=s=s=s=  eroor");
}

System.out.println("------========s=s=s=s=s=");





No comments:

Post a comment