了解使用 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
}
P.s:我的图片链接会像 www.abc.com/images/slider/my/myImage.jpg
最佳答案
使用 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.
有关 URLS 的更多信息,请访问 this
请试一试
关于android - 如何检查字符串是否为有效的 Url 路径格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51146656/