Alaoui Ghita
Alaoui Ghita

Reputation: 1879

android doesn't recognize org.apache.commons.validator

I want to check the validity of a URL address, so after some research, i find that using apache.commons.validator.UrlValidator class is a good solution to do that, this is an example of code :

    import org.apache.commons.validator.UrlValidator;

    public class ValidateUrlExample{

public static void main(String[] args) {

    UrlValidator urlValidator = new UrlValidator();

    //valid URL
    if (urlValidator.isValid("http://www.mkyong.com")) {
       System.out.println("url is valid");
    } else {
       System.out.println("url is invalid");
    }

    //invalid URL
    if (urlValidator.isValid("http://invalidURL^$&%$&^")) {
        System.out.println("url is valid");
    } else {
        System.out.println("url is invalid");
    }

}
}

but android doesn't recognize the class "org.apache.commons.validator" Does anyone know what should I do to fix that ?

Thanks in advance

Upvotes: 2

Views: 3490

Answers (2)

claudio ochoa
claudio ochoa

Reputation: 71

You can use the method isValidUrl() defined in android.webkit.URLUtil. For example

if (!URLUtil.isValidUrl("yourUrl")
   // do something

Upvotes: 7

Mike Bockus
Mike Bockus

Reputation: 2079

If you still want to use the apache commons validator library, then you just need to download the library and package it with your app.

Upvotes: 1

Related Questions