The key code is below:
[Java] view Plaincopyprint?
-
@Override
-
public void Onreceivederror (webview view, int errorcode ,
String description, String failingurl) {
//Use JavaScript to hide system-defined 404 page information
-
string data = " page no found! "
View.loadurl ("javascript:document.body.innerhtml=\" " + data + " \");
}
This article original, reproduced please indicate the source: http://blog.csdn.net/feifei454498130/article/details/23627557
Mainly in Webviewclient, overloaded Onreceivederror methods, and then manipulate the DOM through JavaScript to change the content.
The majority of the online processing method is View.loadurl (point to a Assets directory HTML file or "About:blank"), so that when the refresh will refresh the current page of the error, or in the processing of GoBack () There will be some problems. And the above method can avoid the corresponding problems.
Customize the 404 page. Android's WebView control can load Web pages, WebView has two methods: Webview.setwebchromeclient and Webview.setwebviewclient.
webchromeclient mainly deal with parsing, rendering Web pages and other browsers do things,auxiliary webview Processing JavaScript dialog box, website icon, website title, loading progress:
Onclosewindow (Close webview)
Oncreatewindow ()
Onjsalert (WebView on alert is not something to play, need to customize your webchromeclient processing pop-up)
onjsprompt
onjsconfirm
onprogresschanged
Onreceivedicon
Onreceivedtitle
For example, you can add a progress bar to make the interface more friendly.
webviewclient is to help WebView handle various notifications, request events, specifically including:
onloadresource
onpagestart
onpagefinish
onreceiveerror // This is the way we want.
onreceivedhttpauthrequest
So we're going to load the custom 404 interface with two.
The code is as follows:
 
1 webView = (WebView) findviewbyid ( R.ID.WEBV);//Gets the control 2 webview.getsettings (). Setjavascriptenabled (TRUE); //set JS permissions, such as JS pop-up window, you know 3 webview.getsettings (). Setsupportmultiplewindows (True); 4 5 webview.setscrollbarstyle ( View.scrollbars_inside_overlay); 6 7 webview.setwebchromeclient (New MyWebChromeClient () 8 { 9 public void onprogresschanged (webview view, int progress)//Setup load Process 10 {11 Activity.settitle ("Loading ... Loading ... ");12 activity.setprogress (progress * 100);13 if ( PROGRESS >= 100) {14 new thread (New Runnable () {15 @Override16 public void run () {17 message msg=handler.obtainmessage ();//send notification, join thread 18 msg.what=2;//Loading Complete 19 handler.sendmessage (msg); /notification sent! 20 } 21 }). Start (); 22 activity.settitle (R.string.app_name);23 }24 }25 }26 ); 27 28 Webview.setwebviewclient (New webviewclient () {29 public void onreceivederror (Webview view, int errorcode , string description, string failingurl) 30 { 31 view.stoploading ();32 view.clearview ();33 message msg= Handler.obtainmessage ();//send notification, join thread 34 msg.what=1;//Notice PlusDownload Custom 404 Page 35 handler.sendmessage (msg);//notification sent! 36 }37 public boolean shouldoverrideurlloading ( Webview view, string url) 38 {39 view.loadurl (URL);40 return true;41 }42 }); 43 webview.addjavascriptintErface (this, "Javatojs");//bind this activity to java_js this JS to 44 webview.loadurl (URL); 45 checkuppay ();// Detects if the UnionPay payment control has been added 46 }47 /**48 * handler processing message mechanism 49 */50 protected handler handler = new handler () {51 public void handlemessage (Message Message) {52 switch (message.what) {53 case 0:54 &nBsp; mydialog.show ();55 break;56 case 1:57 Webview.loadurl (URL404);58 break;59 case 2:60 mydialog.dismiss ();61 break;62 }63 }64 };
Note : Although these two methods are system built-in method, but together or there will be a short jump, jump for a moment will see the original 404 error page. I have read the analysis of other articles, some of which think that this is because the WebView loading page is carried out by two threads, so when we receive the error message another thread may display the original page. So there will be a little "flash".
Before using this method I also thought of other loading custom interface, for example, when the page load to greater than 99%, I detect the title of WebView, if it is null or "page not found (different phone may be prompted differently)" Jump to the custom error interface, There will also be a brief flash out of the original 404 interface.
When I found the method of the beginning of the article decisively put this method out, because different phone prompts are not the same, and sometimes Tomcat or other server will return other error messages, this I also want to sense detection, not only slow, also unscientific.
I have limited experience, technology is bad, thin text for reference only. Welcome to Criticize
Android webview Custom processing error page display (404, etc.)