0

I want make a application that work with Internet and a Server and Web service, I create a method to check network available, I use bellow method:

private boolean isNetworkConnect() {
        ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netinfo = cm.getActiveNetworkInfo();
        if (netinfo != null && netinfo.isConnected()) {
            netType = netinfo.getTypeName();
            return true;
        } else {
            return false;
        }
    }

This method just specify that WiFi of Mobile network in Android is Enable or Disable, But this is not sufficient to Internet Access, because if I disable my Laptop WiFi, but my android app in Emulator said Internet in Access because WiFi or Mobile Network is Enable, But if open browser and search any sites is not Access.!

I How to check internet access, that I search on browser, read from server and others. What is best solution ?

Lal
  • 14,726
  • 4
  • 45
  • 70
Saeed Rahmani
  • 650
  • 1
  • 8
  • 29
  • 2
    Check this for better solution - http://stackoverflow.com/questions/1560788/how-to-check-internet-access-on-android-inetaddress-never-timeouts – dhaval Sep 06 '15 at 18:57

3 Answers3

1

Your code is looking good. I already using this code on my own app.

This problem comes because you are using emulator for test your app.

If you test your app on real device isConnected() method work correctly.

If you want to get false return for your isNetworkConnect() method on emulator go to

Settings > Wireless & network > Mobile network And uncheck "Data enabled"

0

Your approach is generally what is used for isOnline method. It does not work by sending packets, thereby verifying connectivity. Its based on the state i.e., whether you are connected to WiFi or Mobile network. Now, as in your case, there may not be any actual connectivity.

To specifically answer your question, this is the best you could do. However, it does not ensure real connectivity.

novic3
  • 106
  • 5
0

Try This(edit as you want)

public void testURL() throws Exception {
String strUrl = "http://stackoverflow.com/about";

try {
    URL url = new URL(strUrl);
    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    urlConn.connect();

    assertEquals(HttpURLConnection.HTTP_OK, urlConn.getResponseCode());
} catch (IOException e) {
    System.err.println("Error creating HTTP connection");
    e.printStackTrace();
    throw e;
}

}

  • I just one question, My method in Emulator just specify that WiFi or Mobile Net. is Enable but in real device good because I Enable WiFi and no Connect to any networks and my app say No Network Access and work right. Why we use HttpUrlConnection? – Saeed Rahmani Sep 06 '15 at 19:41
  • some times connection not responding we are still connected. so we can use `HttpUrlConnection` to check our connection working and responding correctly. (sorry for my bad english :( ) – Hashaŋ Sachiŋtha Sep 06 '15 at 20:28
  • OK, Thank you. (you're welcome :-) ) – Saeed Rahmani Sep 06 '15 at 21:41