Search code examples
androidwebviewandroid-webview

WebView component gives an error (Webpage not available) when navigating to a page whose URL begins with something other than http:// or https://


My Android app uses a WebView to let the user navigate around the Internet, but occasionally it encounters a site that navigates using its own funky schema. For example, if you search for "aliexpress", the app will take you to the site's Home page just fine. enter image description here

However, if you select any of the buttons on the top, WebView throws an error:

enter image description here


Solution

  • All I needed to do was call shouldOverrideUrlLoading() as follows:

                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                  Uri current_page = request.getUrl();
                  if (!current_page.toString().startsWith(("http://")) && !current_page.toString().startsWith(("https://"))) return true;
                  return false;
                }