【问题标题】:Android webview custom error pageAndroid webview自定义错误页面
【发布时间】:2016-03-29 12:23:26
【问题描述】:

我正在创建使用 WebView 访问在线网站的应用程序。我被困在必须添加代码以检查页面可用性的地方。

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);
}

}

我正在尝试添加 onReceivedError,但由于某种原因自定义页面未加载。

/** 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");
}

请告知该怎么做。

【问题讨论】:

  • 在 setWEBChromeClient 中实现 onRecievedError
  • 如果我这样设置 onReceiveError mWebView.setWebChromeClient(new WebChromeClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { mWebView.loadUrl("file:///android_asset/error.html"); } 仍然收到默认错误 - 网页不可用
  • 我添加到 setWebViewClient 及其工作 :) mWebView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { mWebView.loadUrl("file:///android_asset/greska.html"); } });

标签: android webview android-webview


【解决方案1】:

您可以在onReceivedError函数中调用loadErrorPage(view)函数。

下面的代码会加载你需要显示的错误内容。这里我用loadDataWithBaseURL加载html文件。

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();

        }
    }

【讨论】:

  • 在此之前,我需要帮助如何实现 onReceivedError
  • 添加此代码 ---->mWebView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failedUrl) { Log.i("WEB_VIEW_TEST ", "错误代码:" + errorCode); loadErrorPage(view); } }); ======== 这可能会对您有所帮助。
  • 感谢您的帮助,我将代码添加到 mWebView.setWebViewClient(new WebViewClient() 并且它现在正在运行
  • 有没有办法处理被阻止或不安全的网址?如何捕捉这些类型的网址?因为我不认为 onReceivedError 可以捕获被阻止/不安全的 url,因为这些 url 在技术上不是错误的,但它们只是不安全。有没有办法捕捉这些被阻止/不安全的网址?谢谢
【解决方案2】:

我将onReceivedError 添加到mWebView.setWebViewClient(new WebViewClient,所以现在它可以工作了。感谢您的提示。

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

【讨论】:

    【解决方案3】:

    你可以使用下面的代码..

    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);
    
            }
        }
    

    【讨论】:

    • 没有与显示错误信息相关的代码!
    猜你喜欢
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-26
    相关资源
    最近更新 更多