0

I am trying to load the URL to webView on Android Studio. I load the URL based on user input (in my code below is urlResult). Sometimes the user can input in the form of "https://exampleweb.com", or "https://www.exampleweb.com", or "www.exampleweb.com", or "exampleweb.com". Sometimes, the URL that is entered by the user may not load anything (either the link is not active, blocked, does not exist, etc.). When this happens, I would like to show an error HTML page that I already created locally (errorpage.html). How can I achieve this? Thank you!

Here is my code:

public class WebResult extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_result);

        WebView webView = findViewById(R.id.webView_UrlResult);
        webView.setWebViewClient(new WebViewClient(){
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
                webView.loadUrl("file:///android_asset/errorpage.html");
            }
        });


        if (urlResult.startsWith("https://")) {
            webView.loadUrl(urlResult);

        } else {
            webView.loadUrl("https://" + urlResult);
        }


    }
}
2
  • stackoverflow.com/questions/36284186/… Commented Aug 30, 2021 at 6:30
  • Thank you. Is there a way to handle url that is blocked or not safe? How to catch those type of url? Because I don't think the onReceivedError can catch the url that is blocked/not safe since these url are technically not error, but they are just not safe. Is there a method to catch these blocked/not safe url? Thank you Commented Aug 30, 2021 at 8:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.