Android WebView自定义错误页面 [英] Android webview custom error page

查看:1659
本文介绍了Android WebView自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

}

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

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

请告知该怎么做.

推荐答案

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

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

以下代码将加载您需要显示的错误内容.在这里,我使用loadDataWithBaseURL加载html文件.

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

        }
    }

这篇关于Android WebView自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆