当前位置 : 主页 > 手机开发 > android >

android – 当Page Not Available错误时调用WebView onPageFinished.解?

来源:互联网 收集:自由互联 发布时间:2021-06-11
我有一个webview,必须在调用onReceivedError()时生成一个LOG.但问题是,当我有非常糟糕的wifi连接(几乎100%丢失)并且webview显示页面不可用的html错误时,正在调用onPageFinished方法并且未调用onRe
我有一个webview,必须在调用onReceivedError()时生成一个LOG.但问题是,当我有非常糟糕的wifi连接(几乎100%丢失)并且webview显示页面不可用的html错误时,正在调用onPageFinished方法并且未调用onReceivedError

怎么解决这个问题?我希望当页面不可用时,会调用onReceivedError方法!

webView.setWebViewClient(new WebViewClient() {   

    @Override
    public void onPageFinished(WebView view, String url) {
     super.onPageFinished(view, url);
     Log.d("WEBVIEw","ON PAGE FINISHED");
    }    

    @Override
    public void onReceivedError(WebView view, int errorCode,
      String description, String failingUrl) {
      Log.d("WEBVIEw","ON PAGE error");
    }

   });
即使出现错误,也会始终触发onPageFinished.有一个名为onPageSucceeded的方法会很棒 – 但这很容易创建.

在页面顶部创建一个属性private boolean webviewSuccess = true;和

@Override
public void onPageFinished(WebView view, String url) {
 super.onPageFinished(view, url);
 Log.d("WEBVIEW","ON PAGE FINISHED");
 if(webviewSuccess) {
    Log.d("WEBVIEW","ON PAGE SUCCEEDED");
 }
}    

@Override
public void onReceivedError(WebView view, int errorCode,
  String description, String failingUrl) {
  webviewSuccess = false;
  Log.d("WEBVIEW","ON PAGE error");
}
网友评论