Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Server is online or not [NOT WORKING] #324

Closed
Zulqurnain opened this issue Feb 28, 2019 · 10 comments
Closed

Check Server is online or not [NOT WORKING] #324

Zulqurnain opened this issue Feb 28, 2019 · 10 comments

Comments

@Zulqurnain
Copy link

Zulqurnain commented Feb 28, 2019

Server Online or Not
it is always returning false in connectivity no matter what.

To Reproduce
Here is concise function i wrote to check if server is online or not. which is not working

public void observeServerURL(){
      String urlString = "http://www.someserver.com";

      if(urlString == null || urlString.isEmpty())return;
      InternetObservingSettings settings = InternetObservingSettings.builder()
              .strategy(new SocketInternetObservingStrategy())
              .host(urlString)
              .timeout(2000)
              .interval(10 * 1000) // each 10 seconds
              .initialInterval(1000)
              .build();
              ReactiveNetwork
                      .observeInternetConnectivity(settings)
                      .subscribeOn(Schedulers.io())
                      .observeOn(AndroidSchedulers.mainThread())
                      .subscribe(
                              isOnline -> {
                                  LogUtils.d("checking URL status:"+urlString+" , its online:"+isOnline);
                                  }
                              },
                              Throwable::printStackTrace
                      );
}

Smartphone (please complete the following information):

  • Device: OPPO
  • OS: 5.1
  • Library Version: 3.0.2
@pwittchen
Copy link
Owner

pwittchen commented Feb 28, 2019 via email

@Zulqurnain
Copy link
Author

Zulqurnain commented Mar 3, 2019

@pwittchen yeah I've checked Walled strategy its giving same results

@Zulqurnain
Copy link
Author

Any news on this , i was going to integrate this in my app ?

@pwittchen
Copy link
Owner

pwittchen commented Mar 10, 2019

I couldn't reproduce your issue. Maybe it's an issue in your app? You can verify that by running sample projects from this repo. If they works for you, then it must be an issue with your app. Moreover, I don't know what cpp.add(...) stands for in your context. Maybe this is a source of the problems?

@Zulqurnain
Copy link
Author

Zulqurnain commented Mar 11, 2019

Ok, I updated the code and removed that cpp.add() , because it was just a CompositeDisposable of rxjava , can you try on some test server except google ? because for my server it always give response in logcat for [getaddrinfo]: hostname=myservername.com; servname=(null); netid=0; mark=0 like this

@pwittchen
Copy link
Owner

pwittchen commented Mar 11, 2019

Then something may be wrong with your server. Moreover, in InternetObservingSettings you can set httpResponse(200) (status: OK) if this is response status of your server. Default response in the settings is HttpURLConnection.HTTP_NO_CONTENT (204), which is the case in pinging http://clients3.google.com/generate_204 in WalledGardenInternetObservingStrategy.

@pwittchen
Copy link
Owner

@Zulqurnain Did setting HTTP status helped in your case?

@Zulqurnain
Copy link
Author

I just got an update from my backend team, they have disabled pinging on the server, is it somehow related to the issues?

@pwittchen
Copy link
Owner

Yes. This feature of the library pings the server.

@Zulqurnain
Copy link
Author

I have found a solution for now, below code works with my server

private static boolean isURLReachable(String serverURL) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if(cm == null)return false;

        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            try {
                URL url = new URL(serverURL);   // Insert Url
                HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                urlc.setConnectTimeout(10 * 1000);          // 10 s.
                urlc.connect();
                if (urlc.getResponseCode() == 200) {        // 200 = "OK" code (http connection is fine).
                    Log.wtf("Connection", "Success !");
                    return true;
                } else {
                    return false;
                }
            } catch (MalformedURLException e1) {
                return false;
            } catch (IOException e) {
                return false;
            }
        }
        return false;
    }

but the library doesn't work, rest is up to you @pwittchen, hope you can debug the issue or add the functionality for server online detect for the server who have pinging disabled.

but my issue is not resolved by this library !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants