Basic Android tutorial -- 7.5.6 WebView: process error code returned by a Web page, 7.5.6webview

Source: Internet
Author: User

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.

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.