I am trying to check server connectivity on splash screen. I want make if server is online then I want download data else if server does not ping, I need to show error. I am trying to user function for server status check like below
static public boolean isServerReachable(Context context) {
ConnectivityManager connMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = connMan.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
try {
URL urlServer = new URL("your server url");
HttpURLConnection urlConn = (HttpURLConnection) urlServer.openConnection();
urlConn.setConnectTimeout(3000); //<- 3Seconds Timeout
urlConn.connect();
if (urlConn.getResponseCode() == 200) {
return true;
} else {
return false;
}
} catch (MalformedURLException e1) {
return false;
} catch (IOException e) {
return false;
}
}
return false;
}
but I do not know how can I use this method. I am trying like below code
boolean isServerReachable;
if (isServerReachable=true){
}
else
{
}
But this showing me that boolean isServerReachable; is never used.
if I try like this
boolean isServerReachable;
if(isServerReachable(SplashsActivity.this)){
if (isServerReachable==true){
}
else {
}
}
Can someone please help me what's I am missing ? Maybe this is very simple and foolish question but I am learning yet, so please help me. Thanks