When a 404 path is loaded in webview, content such as not found is displayed, but sometimes we need to do other processing after such a problem occurs. All the errors we want to capture will be displayed, however, the SDK's error handling method does not call back after loading the error:
Public void onreceivederror (webview view,
Int errorcode, string description, string failingurl) since: API Level 1
Report an error to the Host application. These errors are unrecoverable (I. e. the main resource is unavailable). The errorcode parameter corresponds to one of the error _ * constants.
Parameters
| View |
The webview that is initiating the callback. |
| Errorcode |
The error code corresponding to an error _ * value. |
| Description |
A string describing the error. |
| Failingurl |
The URL that failed to load. |
No way, we have to get used to the problem of the SDK. The solution below is to make a processing during loading and reconnect the URL to see what the returned code is, and then proceed as follows:
private void checkWebViewUrl(final WebView webView, final String url) {if (url==null||url.equals("")) {return;}new AsyncTask<String, Void, Integer>() {@Overrideprotected Integer doInBackground(String... params) {int responseCode = -1;try {URL url = new URL(params[0]);HttpURLConnection connection = (HttpURLConnection) url.openConnection();responseCode = connection.getResponseCode();} catch (Exception e) {log.e("Loading webView error:" + e.getMessage());}return responseCode;}@Overrideprotected void onPostExecute(Integer result) {if (result != 200) {webView.setVisibility(View.GONE);} else {webView.loadUrl(url);}}}.execute(url);}
Note: The onreceivederror method is enabled only when the network status of the device is disconnected. If the server corresponding to the URL path returns 404, the method is not enabled.