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