boudi boudi - 10 days ago 6
Android Question

Validate Specfic URL input in EditText for android

My application is about downloading an image from a specific website e.g.

www.example.com/img-...


The user will input the url for the img to the EditText field. e.g.
www.example.com/img-123


My problem is that when the user inputs a wrong URL, i.e. one with no no image, it is empty e.g.
www.example.com/img-222


I want to detect this and tell the user their input does not link to an image and try again.

I'm using the
isValidUrl()
function to detect if the input is a WEB_URL only but what I want is that when the entered url has no image, the program should tell them it is an incorrect format for url.

I'm using Jsoup.connect(url).get(); to connect to the url and get the image and save it

private boolean isValidUrl(String url) {
Pattern p = Patterns.WEB_URL;
Matcher m = p.matcher(url);
if(m.matches())
return true;
else
return false;
}

Answer
String[] schemes = {"http","https"}; //DEFAULT schemes = "http", "https", "ftp"

UrlValidator urlValidator = new UrlValidator(schemes);

if (urlValidator.isValid("http://www.google.com")) {
    //url is valid
}else{
    //url is invalid
}

Use Apache commons-validator URLValidator class