Basic Android tutorial -- 7.5.6 WebView: process error code returned by a Web page, 7.5.6webview
Basic Android tutorial -- 7.5.6 WebView
Tags (separated by spaces): basic Android tutorial
This section introduces:
Hey, if your company is an HTML5 mobile APP, you can use WebView to display web pages.
Does not exist, or other errors. The status code 404,401,403, 30X, and other errors are reported. If the default WebView error is displayed
The prompt page may seem unfriendly. We can rewrite the onReceivedError () method of WebViewClient to implement our
There are two common methods to achieve the desired effect. One is to create an error message in the assets Directory.
In the HTML page, when an error occurs, that is, when onReceivedError () is called, we call the loadUrl of webView to jump to us.
For example, wView. loadUrl ("file: // android_asset/error.html ");! Or we can write
A layout or a large image is usually set to invisible. When a page error occurs, make the layout or image visible!
Below is a simple example!
1. webpage error. load custom webpage:
Run:
Key code:
WView. setWebViewClient (new WebViewClient () {// set the new webpage opened when you click in webView to be displayed on the current page without redirecting to the new browser @ Override public boolean shouldOverrideUrlLoading (WebView view, string url) {view. loadUrl (url); return true ;}@ Override public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {super. onReceivedError (view, errorCode, description, failingUrl); wView. loadUrl ("file: // android_asset/error.html ");}});
2. The page is incorrect and the corresponding View is displayed.
Run:
Implementation Code:
Public class MainActivity extends AppCompatActivity implements View. onClickListener {private WebView wView; private ImageView img_error_back; private Button 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 (new WebViewClient () {// set the new webpage opened when you click in webView to be displayed on the current page without redirecting to the new browser @ Override public boolean shouldOverrideUrlLoading (WebView view, string url) {view. loadUrl (url); return true ;}@ Override public void onReceivedError (WebView view, int errorCode, 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. Download the sample code:
WebViewDemo8.zip: Http://pan.baidu.com/s/1c02lNGW
Summary:
Well, it's a simple section. Haha, it's super simple. In addition, we can set different rules based on different errorcodes.
Page ~ Here we will expand it by ourselves. The basic learning about WebView is here. At the beginning of the next section, we will welcome network programming.
One difficulty: Socket network programming. Of course, if you have learned it, it will be easy to learn. It doesn't matter if you haven't learned it. Pig will take you into consideration.
Socket ~ Coming soon ~ I am not used to posting an emoticons. Haha ~
Thank you ~
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.