boudi boudi - 1 year ago 114
Android Question

Validate Specfic URL input in EditText for android

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

The user will input the url for the img to the EditText field. e.g.

My problem is that when the user inputs a wrong URL, i.e. one with no no image, it is empty e.g.

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

I'm using the
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);
return true;
return false;

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

UrlValidator urlValidator = new UrlValidator(schemes);

if (urlValidator.isValid("")) {
    //url is valid
    //url is invalid

Use Apache commons-validator URLValidator class

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download