Android webview error handling

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.