For reprint, please indicate the source. For rights protection: https://www.cnblogs.com/tangZH/p/9913968.html
Rewrite the methods in WebViewClient, and then WebView.setWebViewClient(mWebViewClient);
WebViewClient mWebViewClient = new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {return false; } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); } @Override public void onLoadResource(WebView webView, String s) { super.onLoadResource(webView, s); } @Override public void onScaleChanged(WebView webView, float v, float v1) { super.onScaleChanged(webView, v, v1); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
mWebView.loadUrl("");//Avoid default error pages showErrorView(); } };
In onReceivedError, you can prompt and handle network errors. If you go here, you will find network errors or server errors. Whether there is no network can be judged in this method. This method can be used to listen to callbacks for those above 6.0 and below.
(practice proves that:
1. After this method is called, the onPageFinished() method will also be called back, so it is better not to reset the page in this method, for example, when the network is disconnected, the disconnection page will pop up, but if the normal page is displayed in onPageFinished(), the disconnection prompt will disappear.
2. onPageFinished() will be called multiple times)
However, a new method has been added at the time of 6.0 and above:
@Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) { super.onReceivedError(view, request, error); }
It has been proved that this method will be called back when the web page is loaded successfully, or when other non network errors occur, so we need to determine what errors are through error and filter them.