Android WebView onReceivedError ()

Does anyone know if there is a way to intercept the "page not found" or the "error loading page" in WebView?

According to the Android documentation, onReceivedError() should be able to intercept. but I tested it in an application that I deleted incorrectly but did nothing.

I want my application to be able to provide its own error message if the URL is ever inaccessible for any reason.

this is the code that did nothing:

 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // custom error handling ... show and alert or toast or something } 
+29
android webview
Feb 14 2018-11-21T00:
source share
3 answers

According to the documentation and my experience, it should work well. You just need to install WebClient using the override onReceivedError method in your WebView.

Here is a snippet from my old test application:

  WebView wv = (WebView) findViewById(R.id.webView); wv.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Log.i("WEB_VIEW_TEST", "error code:" + errorCode); super.onReceivedError(view, errorCode, description, failingUrl); } }); 

I tested it and it works fine. Check your logs and see what code error you get. Hope this helps.

+29
Feb 14 '11 at 10:24
source share

I tried using onReceivedError both inside shouldOverrideUrlLoading () and outside of this method, but in a WebViewClient. I even tried in the main class Activity. I was dissatisfied with the inconsistent results. Therefore, I decided to use the test method isOnline () and call it before calling loadUrl ().

 public boolean isOnline() { ConnectivityManager cm = (ConnectivityManager) getBaseContext() .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo i = cm.getActiveNetworkInfo(); if ((i == null) || (!i.isConnected())) { Toast toast = Toast.makeText(getBaseContext(), "Error: No connection to Internet", Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0); toast.show(); return false; } return true; } 

Then this onReceivedError is in the WebViewClient, but outside the overloadurltingy method. This seems to constantly prevent the dumb, grinning android error pages.

  @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { if (view.canGoBack()) { view.goBack(); } Toast toast = Toast.makeText(getBaseContext(), description, Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 0); toast.show(); } 

Some people may find this resource difficult. Well, not as hard as Android Android and Google+ apps. And not Google services. I honestly don't mind using a little oxygen. Call me a bad guy ...

+10
Apr 13 '13 at 20:55
source share

You should use this after completion on the page.

  @Override public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){ //Your code to do Toast.makeText(getActivity(), "Your Internet Connection May not be active Or " + error , Toast.LENGTH_LONG).show(); } 
+1
May 10 '16 at 17:46
source share



All Articles