20

I am creating application that use WebView to access a online website. I am stuck where I have to add code to check availability of page.

public class SpartanWeb extends Activity {

WebView mWebView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Adds Progrss bar Support
    this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.main);

    // Makes Progress bar Visible
    getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
            Window.PROGRESS_VISIBILITY_ON);

    // Get Web view
    mWebView = (WebView) findViewById(R.id.webView1);
    WebSettings websettings = mWebView.getSettings();
    websettings.setJavaScriptEnabled(true);
    mWebView.stopLoading();
    mWebView.clearCache(true);
    mWebView.loadUrl("http://google.com");
    mWebView.setHorizontalScrollBarEnabled(false);
    mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.setDownloadListener(new DownloadListener() {
        public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
        }
    });

    // onProgressChanged
    final Activity MyActivity = this;
    mWebView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            // bar disappear after URL is loaded, and changes string to
            // Loading...
            MyActivity.setTitle("Loading...");
            MyActivity.setProgress(progress * 100); // Make the bar
                                                    // disappear after URL
                                                    // is loaded

            // Return the app name after finish loading
            if (progress == 100)
                MyActivity.setTitle(R.string.app_name);
        }
    });

}// EOM oc

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
        mWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

}

I am trying to add onReceivedError but for some reason custom page is not loading.

/** Called when the activity is first created. */
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) 
{
mWebView.loadUrl("file:///android_asset/error.html");
}

Please advise what to do.

3
  • 1
    implement your onRecievedError in setWEbChromeClient Commented Mar 29, 2016 at 12:31
  • if i put onReceiveError like this mWebView.setWebChromeClient(new WebChromeClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { mWebView.loadUrl("file:///android_asset/error.html"); } still receiving default error - webpage not available Commented Mar 29, 2016 at 13:08
  • I added to setWebViewClient and its working :) mWebView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { mWebView.loadUrl("file:///android_asset/greska.html"); } }); Commented Mar 29, 2016 at 13:46

3 Answers 3

23

You can call loadErrorPage(view) function in the onReceivedError function.

The following code will load the error content you need to show.Here i am load the html file with loadDataWithBaseURL.

public void loadErrorPage(WebView webview){
        if(webview!=null){

            String htmlData ="<html><body><div align=\"center\" >"This is the description for the load fail : "+description+"\nThe failed url is : "+failingUrl+"\n"</div></body>";

            webview.loadUrl("about:blank");
            webview.loadDataWithBaseURL(null,htmlData, "text/html", "UTF-8",null);
            webview.invalidate();

        }
    }
Sign up to request clarification or add additional context in comments.

5 Comments

Before that, i need help how to implement onReceivedError
add this code ---->mWebView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Log.i("WEB_VIEW_TEST", "error code:" + errorCode); loadErrorPage(view); } }); ======== This may help you.
thanks for help, i added my code to mWebView.setWebViewClient(new WebViewClient() and it is working now
Is there a way to handle url that is blocked or not safe? How to catch those type of url? Because I don't think the onReceivedError can catch the url that is blocked/not safe since these url are technically not error, but they are just not safe. Is there a method to catch these blocked/not safe url? Thank you
It's working fine but if the user go to destination page after connecting to internet, and on back press this error page is displaying. How to remove this from error page in backward history..?
12

I added onReceivedError to mWebView.setWebViewClient(new WebViewClient so now it's working. Thanks for tips.

mWebView.setWebViewClient(new WebViewClient() { 
        @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
               mWebView.loadUrl("file:///android_asset/error.html");
        } });

Comments

-6

You can use the following code ..

public class TestResultWebclient extends WebViewClient {
        ProgressDialog progressDialog;

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (progressDialog == null) {
                progressDialog = new ProgressDialog(TermsAndCondsMrupeeActivity.this);
                progressDialog.setMessage("Loading...");
                progressDialog.show();
            }
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (progressDialog != null)
                try {

                    if (progressDialog.isShowing()) {
                        progressDialog.dismiss();
                        progressDialog = null;
                    }

                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            super.onPageFinished(view, url);

        }
    }

1 Comment

There is no code relating to showing the error message!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.