-1

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 {

            }
        }
its showing variable isServerReachable might not been initialized

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

Meera Joshi
  • 59
  • 1
  • 7
  • 1
    Try it using `if (isServerReachable)` or `if (isServerReachable==true)`probably work :) – ρяσѕρєя K Jan 15 '17 at 04:42
  • @ρяσѕρєяK its showing me error that can not resolve symbol isServerReachable – Meera Joshi Jan 15 '17 at 04:43
  • 1
    ohhh, use `if(isServerReachable(Context of current class))` both bool variable and method name is same – ρяσѕρєя K Jan 15 '17 at 04:44
  • @MeeraJoshi follow these links http://stackoverflow.com/questions/1443166/android-how-to-check-if-the-server-is-available and http://stackoverflow.com/questions/18321118/best-alternative-for-inetaddress-getbynamehost-isreachabletimeout/18845416#18845416 – Saurabh Bhandari Jan 15 '17 at 04:52
  • @SaurabhBhandari But How should I call this method ? – Meera Joshi Jan 15 '17 at 04:55
  • you have to pass your host name, and context to thr Boolean variable. then check it using if else. I post two links in above comment just follow them – Saurabh Bhandari Jan 15 '17 at 04:59
  • To check if flag is true, `if (flag)`, not `if (flag=true)` (that will ASSIGN true to flag because only one `=`), and `if (flag == true)` is a waste. So just say `if (isServerReachable) { ... }` – clearlight Jan 15 '17 at 05:02
  • @MeeraJoshi first you need check your mobile data or wifi is on if they are not enable then how can you check server responding or not – Saurabh Bhandari Jan 15 '17 at 05:03
  • 1
    @MeeraJoshi: just write `boolean isServerReachable=isServerReachable(SplashsActivity.this);if (isServerReachable==true){}else{}` – ρяσѕρєя K Jan 15 '17 at 05:04
  • @ρяσѕρєяK...Thanks...its working fine as you have commented...Thanks a lot – Meera Joshi Jan 15 '17 at 05:08

2 Answers2

0

You can call the method inside if condition -

if(isServerReachable(context_value))
{

} 
else 
{

}

The code you are currently using (given below) doesn't call the method. It actually assign true value to variable - isServerReachable, which is never used in 'if'or 'else' block

boolean isServerReachable;

    if (isServerReachable=true){

    }
    else
    {

    }
0

If boolean isServerReachable is a local variables then it don't get default values, they have to be initialized. ... Local variables should be initialized with value before using it .Something like this :boolean isServerReachable = false The compiler complains because local variables are not assigned any value by default.

boolean isServerReachable = false;
    if(isServerReachable(SplashsActivity.this)){
        if (isServerReachable==true){

        }
        else {

        }
    }

It will fix that its showing variable isServerReachable might not been initialized issue.

Mohd. Shariq
  • 214
  • 3
  • 14