3

I am loading url in android webview using below code

webviewShowPost.loadUrl(URL);

I want to check if no data connectivity available then webview instead of showing blank view, I can display Toast of no connectivity.

Thanks

Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118

5 Answers5

9

Please try this

webView.setWebViewClient(new WebViewClient() { 
        @Override
        public void onReceivedError(WebView view, int errorCode,
                String description, String failingUrl) {
            // TODO Auto-generated method stub
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);
        }


    });
Sandeep Kumar P K
  • 7,412
  • 6
  • 36
  • 40
2
public static boolean isOnline(Context context) {

        try {
            ConnectivityManager cm = (ConnectivityManager) context

            .getSystemService(Context.CONNECTIVITY_SERVICE);

            if (cm.getActiveNetworkInfo().isConnectedOrConnecting()) {

                URL url = new URL("http://www.google.com.pk/");
                                HttpURLConnection urlc = (HttpURLConnection) url
                        .openConnection();
                urlc.setConnectTimeout(1000); // mTimeout is in seconds

                urlc.connect();

                if (urlc.getResponseCode() == 200) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }
SALMAN
  • 2,031
  • 1
  • 20
  • 18
  • 2
    This code checks connectivity from server and response it is more reliable than connectivity manager because in some cases wifi is enabled but isnt able to connect to server or can get any response. Thanks. – SALMAN Dec 02 '11 at 06:50
2

you can get the Progress Percentage value in above webview method

 mWebView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress)  
        {
         //Make the bar disappear after URL is loaded, and changes string to Loading...
     //Make the bar disappear after URL is loaded
         System.out.println("Value of progress"+progress);
            pbweb.setProgress(progress);

            if(progress == 100)


            pbweb.setVisibility(View.GONE);
          }
        });

below code in progress is value of progerss

Hardik Gajjar
  • 5,038
  • 8
  • 33
  • 51
  • In some cases, even if progress is 100%, it is not guaranteed that the page has loaded completely. Check this question http://stackoverflow.com/q/6199717/840669 – Rajkiran Mar 18 '15 at 09:18
0

You want to check if there is network connectivity before loading the page, which means you want to do this: https://stackoverflow.com/a/2001824/960048

Community
  • 1
  • 1
Salil Pandit
  • 1,498
  • 10
  • 13
0

you can always use WebViewClient for this purpose .

    web.setWebViewClient(new WebViewClient(){

         public void onReceivedError(WebView view, int errorCode, String description,String failingUrl) {
             Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
         }
    });
Sujit
  • 10,512
  • 9
  • 40
  • 45