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 }
Android WebView is used to display HTML in an android app. We can use android WebView to load HTML page into android app.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With