Basic Android tutorial -- 7.5.6 WebView
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 );}}