Android Basics Getting Started tutorial--7.5.6 webview handling the error code information returned by the Web page

Source: Internet
Author: User
Tags error status code

Android Basics Getting Started tutorial--7.5.6 webview handling the error code information returned by the Web page

tags (space delimited): Android Basics Getting Started Tutorial

Introduction to this section:

Hey, if your company is doing HTML5 mobile app, is to display the page through WebView, if you visit the Web page
Does not exist, or other error, reported 404,401,403,30x such as Error status code, if the direct popup webview default error
Tip page, which may seem less friendly, we can rewrite Webviewclient's Onreceivederror () method to implement our
The desired effect, the general approach has two, one is: we ourselves in the assets directory to create a display error message
HTML page, when an error occurs, that is, Onreceivederror () is called when we call WebView's loadurl to jump to our
Error page, for example: Wview.loadurl ("file:///android_asset/error.html");! Or we'll write another
A layout or directly a large picture, usually set to not visible, when the page is wrong, let the layout or picture Visible!
Let's write a simple example!

1. Page error, loading custom Web page:

Run :

Key Code :

Wview.setwebviewclient (NewWebviewclient () {//Set in WebView Click to open the new page displayed in the current screen without jumping to the new browser    @Override         Public Boolean shouldoverrideurlloading(WebView view, String URL) {view.loadurl (URL);return true; }@Override         Public void Onreceivederror(WebView view,intErrorCode, string description, String failingurl) {Super. Onreceivederror (view, ErrorCode, description, Failingurl); Wview.loadurl ("File:///android_asset/error.html"); }        });
2. Page error, display the corresponding view

Run :

Implementation Code :

 Public  class mainactivity extends appcompatactivity implements View . Onclicklistener {    PrivateWebView Wview;PrivateImageView Img_error_back;PrivateButton Btn_refresh;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        Wview = (WebView) Findviewbyid (R.id.wview);        Img_error_back = (ImageView) Findviewbyid (r.id.img_error_back);        Btn_refresh = (Button) Findviewbyid (R.id.btn_refresh); Wview.loadurl ("Http://www.baidu.com"); Wview.setwebviewclient (NewWebviewclient () {//Set in WebView Click to open the new page displayed in the current screen without jumping to the new browser            @Override             Public Boolean shouldoverrideurlloading(WebView view, String URL) {view.loadurl (URL);return true; }@Override             Public void Onreceivederror(WebView view,intErrorCode, string description, String failingurl) {Super. Onreceivederror (view, ErrorCode, description, Failingurl);                Wview.setvisibility (View.gone);            Img_error_back.setvisibility (view.visible);        }        }); Btn_refresh.setonclicklistener ( This); }@Override     Public void OnClick(View v) {Wview.loadurl ("Http://www.baidu.com");        Img_error_back.setvisibility (View.gone);    Wview.setvisibility (view.visible); }}
3. Sample code Download:

webviewdemo8.zip: Http://pan.baidu.com/s/1c02lNGW

This section summarizes:

Well, a very simple bar, haha, super simple is it, in addition we can also according to different errorcode to set different
Page ~ Here to expand their own, about WebView Basic learning is here, the next section begins we will usher in network programming
In a difficult point: socket network programming, of course, if you learn, nature is easy to learn, did not learn it's OK, piglet take you
socket~ please look not to put an expression not used to, haha ~

Thank you ~

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Basics Getting Started tutorial--7.5.6 webview handling the error code information returned by the Web page

Related Article

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.