http post - Check Internet Connection in Android (Not connection State) -
i need check if there's connection given url make sure have connection internet.
i've used code:
private boolean isnetworkavailable() { connectivitymanager connectivitymanager = (connectivitymanager) getsystemservice(context.connectivity_service); networkinfo activenetworkinfo = connectivitymanager.getactivenetworkinfo(); return activenetworkinfo != null && activenetworkinfo.isconnected(); }
but it's not helpful, cause device may connected wifi or mobile network there's no connection internet.
please help
you can ping website checking if internet available. can ping google.com, online.
public static boolean ping(string url, int timeout) { url = url.replacefirst("https", "http"); // otherwise exception may thrown on invalid ssl certificates. try { httpurlconnection connection = (httpurlconnection) new url(url).openconnection(); connection.setconnecttimeout(timeout); connection.setreadtimeout(timeout); connection.setrequestmethod("head"); int responsecode = connection.getresponsecode(); return (200 <= responsecode && responsecode <= 399); } catch (ioexception exception) { return false; } }
Comments
Post a Comment