How to check if URL is valid in Android

后端 未结 12 1679
轻奢々
轻奢々 2020-11-27 02:31

Is there a good way to avoid the \"host is not resolved\" error that crashes an app? Some sort of a way to try connecting to a host ( like a URL ) and see if it\'s even vali

12条回答
  •  情书的邮戳
    2020-11-27 03:24

    I would use a combination of methods mentioned here and in other Stackoverflow threads:

    public static boolean IsValidUrl(String urlString) {
        try {
            URL url = new URL(urlString);
            return URLUtil.isValidUrl(urlString) && Patterns.WEB_URL.matcher(urlString).matches();
        } catch (MalformedURLException ignored) {
        }
        return false;
    }
    

提交回复
热议问题