1

After trying and searching on stackoverflow for ways to solve internet connection error on android, I found nothing what works for me. I tryed the code you can see at the bottom but it wont works for internet connection error. mWebView.loadUrl("file:///android_asset/myerrorpage.html"); become executed every time when the url in the browser isnt http://192./loc/index.php. When I have a redirecting at index.php the error file become showed. How can I change that, or know anyone a code that check the internet connection availability and then do something?

    public boolean isOnline() {
            ConnectivityManager cm =
                (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = cm.getActiveNetworkInfo();
            return netInfo != null && netInfo.isConnectedOrConnecting();
        }

    public boolean isInternetAvailable() {
            try {
                InetAddress ipAddr = InetAddress.getByName("google.com"); 
                if (ipAddr.equals("")) {
                    return false;
 mWebView.loadUrl("file:///android_asset/myerrorpage.html");
                } else {
                    return true;

@Override
        public void onCreate(Bundle savedInstanceState) {


            setContentView(R.layout.activity_localy);
            mWebView = (WebView) findViewById(R.id.webview);
            // Brower niceties -- pinch / zoom, follow links in place
            mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
            mWebView.setWebViewClient(new GeoWebViewClient());
            // Below required for geolocation
            mWebView.getSettings().setJavaScriptEnabled(true);
            mWebView.getSettings().setGeolocationEnabled(true);
            mWebView.setWebChromeClient(new GeoWebChromeClient());     
            // Load google.com
            mWebView.loadUrl("http://192./loc/index.php");


        }
                }
            } catch (Exception e) {
                return false;
            }

        }
Droidboyx
  • 87
  • 1
  • 10
  • Ok, your ip address looks wrong, it is a quad dotted notation, ie. w.x.y.z. for starters. You need to add extra code to check if there's connectivity, you did search? there's one here on the [sidebar to the right --->](http://stackoverflow.com/questions/4238921/detect-whether-there-is-an-internet-connection-available-on-android?rq=1) – t0mm13b Oct 25 '15 at 11:21
  • I edit my ip adress for stackoverflow – Droidboyx Oct 25 '15 at 11:25
  • 192.168.x.y or 192.169.x.y is common as muck with home LAN's, not much to give away there as you would be behind a public IP address anyway. – t0mm13b Oct 25 '15 at 11:27

1 Answers1

2

Your question isn't very clear. If you need check connection you need:

public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnectedOrConnecting();
}

Then if need check for real internet connection you can try something like this:

public boolean isInternetAvailable() {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com"); 
            if (ipAddr.equals("")) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            return false;
        }

    }

And add the ACCESS_NETWORK_STATE permission to the manifest.

granmirupa
  • 2,780
  • 16
  • 27
  • Ok I edit my code a bit. Where can I add my code into your second code snippet or where can I add your second code snippet in my code? – Droidboyx Oct 25 '15 at 11:29
  • 1
    I think you need call first isOnline method. If it return true you can call isInternetAvailable method. You can add where you need to use it. – granmirupa Oct 25 '15 at 11:39
  • You need to call the methods in your onCreate method otherwise thay never will work. Also you need to load your page: "myerrorpage.html" before the return statement otherwise it will never call. – granmirupa Oct 25 '15 at 12:19