当Page Not Available错误时,将调用WebView onPageFinished。解?

时间:2014-05-21 10:09:47

标签: android webview android-webview

我有一个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");
    }

   });

1 个答案:

答案 0 :(得分:7)

即使出现错误,也会始终触发

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