且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

机器人的WebView onReceivedError()

更新时间:2023-11-18 23:39:52

据文件和我的经验,它应该工作相当精细。 你只需要设置你的 Web客户端在你的WebView覆盖方法 onReceivedError

下面是我的一些旧的测试应用程序的代码片段:

 的WebView WV =(web视图)findViewById(R.id.webView);
 wv.setWebViewClient(新WebViewClient(){
    @覆盖
    公共无效onReceivedError(web视图来看,INT错误code,字符串描述,字符串failingUrl){
            Log.i(WEB_VIEW_TEST,错误code:+错误code);
            super.onReceivedError(查看,错误code,说明,failingUrl);
    }
 });
 

我测试过它,它工作得很好。检查你的日志,看看你得到什么样的code错误。 希望它能帮助。

Does anyone know if there is a way to intercept a "page not found" or "page not loading error" in WebView?

According to the android documentation, onReceivedError() should be able to intercept. but i tested it in an app which I deleberately gave the wrong URL, and it didn't do anything.

I want my app to be able to give my own custom error message if the URL is ever unavailable for any reason.

this is the code that did nothing:

public void onReceivedError(WebView view, int errorCode,
        String description, String failingUrl) {

    // custom error handling ... show and alert or toast or something
}

According to documentation and my experience it should work quite fine. You just have to set your WebClient with overriden method onReceivedError in your WebView.

Here is the snippet from some of my old test app:

 WebView wv = (WebView) findViewById(R.id.webView);
 wv.setWebViewClient(new WebViewClient() {
    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Log.i("WEB_VIEW_TEST", "error code:" + errorCode);
            super.onReceivedError(view, errorCode, description, failingUrl);
    }
 });

I've tested it and it works quite fine. Check your logs and see what kind of code error do you get. Hope it helps.