如何检查字符串是否有效的网址路径格式

时间:2018-07-03 04:24:44

标签: android validation url

理解,使用Patterns.WEB_URL.matcher(url).matches()可以验证该字符串是否为有效的url,但是需要使用包含https:/ .com的完整格式。

就我而言,我想验证json是否以正确的格式返回路径的字符串,例如/images/slider/my/myImage.jpg;其中不包含https或任何其他内容。我该怎么办?

我想做的事情是这样的:

if(ImageUrl equal "/images/slider/my/myImage.jpg" FORMAT) {
     //Do something here
} else //ImageUrl = myImage.jpg {
    //Add "/images/slider/my/" infront of the text
}

附言:我的图片链接将类似于www.abc.com/images/slider/my/myImage.jpg

1 个答案:

答案 0 :(得分:1)

使用URLUtil如下验证URL。

 URLUtil.isValidUrl(url)

如果URL有效,则返回True;如果URL无效,则返回false。

下面提供了另一种方法。

public static boolean checkURL(CharSequence input) {
    if (TextUtils.isEmpty(input)) {
        return false;
    }
    Pattern URL_PATTERN = Patterns.WEB_URL;
    boolean isURL = URL_PATTERN.matcher(input).matches();
    if (!isURL) {
        String urlString = input + "";
        if (URLUtil.isNetworkUrl(urlString)) {
            try {
                new URL(urlString);
                isURL = true;
            } catch (Exception e) {
            }
        }
    }
    return isURL;
}

This link will explain how you can check the url is available or not.

有关URL的更多信息,请访问this

请尝试

相关问题