Shawn.Y
Shawn.Y

Reputation: 181

How to check if the string is valid Url path format

Understands that using Patterns.WEB_URL.matcher(url).matches() will able to validate if that string is a valid url, however it will required in a full format which contain https: / .com.

In my case, i want to validate if the json return a string in correct format for path e.g /images/slider/my/myImage.jpg; which does not contain https or any. How can i do this?

What i want to do is something like:

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: My image link will be like www.abc.com/images/slider/my/myImage.jpg

Upvotes: 1

Views: 3324

Answers (1)

Pranav MS
Pranav MS

Reputation: 2296

Use URLUtil to validate the URL as below.

 URLUtil.isValidUrl(url)

It will return True if URL is valid and false if URL is invalid.

Another way is given below.

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.

For more about URLS please visit this

Please have a try

Upvotes: 1

Related Questions