Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView onReceivedError()

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

According to the android documentation, onReceivedError() should be able to intercept. but i tested it in an app which I deleberately gave the wrong URL, and it didn't do anything.

I want my app to be able to give my own custom error message if the URL is ever unavailable 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 } 
like image 826
Joe Winfield Avatar asked Feb 14 '11 21:02

Joe Winfield


People also ask

What is the use of WebViewClient in Android?

Android WebView is used to display HTML in an android app. We can use android WebView to load HTML page into android app.

What is a WebViewClient?

WebViewClient is the object responsible for most the actions inside a WebView. JavaScript enabled, security, routing, etc. You can make a custom one, as well as use a Chrome one.

What is shouldOverrideUrlLoading?

shouldOverrideUrlLoading is called when a new page is about to be opened whereas shouldInterceptRequest is called each time a resource is loaded like a css file, a js file etc.

How do you address WebView SSL error handler alerts in your apps?

Sign in to your Play Console, and navigate to the Alerts section to see which apps are affected and the deadlines to resolve these issues. Update your affected apps and fix the vulnerability. Submit the updated versions of your affected apps.


1 Answers

According to documentation and my experience it should work quite fine. You just have to set your WebClient with overriden method onReceivedError in your WebView.

Here is the snippet from some of my old test app:

 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've tested it and it works quite fine. Check your logs and see what kind of code error do you get. Hope it helps.

like image 166
androdevo Avatar answered Oct 05 '22 11:10

androdevo