Android webview custom error page

前端 未结 3 477
北荒
北荒 2021-02-05 12:41

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         


        
相关标签:
3条回答
  • 2021-02-05 13:01

    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);
    
            }
        }
    
    0 讨论(0)
  • 2021-02-05 13:07

    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();
    
            }
        }
    
    0 讨论(0)
  • 2021-02-05 13:21

    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");
            } });
    
    0 讨论(0)
提交回复
热议问题