Search code examples
javavalidationurlpattern-matchingcodenameone

Url pattern matching in Codename One for validating urls


My Android app performs some checks on urls when opening fails.

Now I am trying to port it to iOS by Codename One.

My code is below. Note that Android code is inserted, I would like to have the Codename counterpart:

Boolean can = Display.getInstance().canExecute(url);
    if(can != null && can) {
        Display.getInstance().execute(url);
    } else {
        boolean validWebUrl =
                Patterns.WEB_URL.matcher(url).matches(); //Android code, won't compile
        if (validWebUrl)
        {
//shows a message about activity/app not available
        else {
//shows a message about the url being not valid
        }
    }

As you can see the user is informed about the result, after the check, if it makes sense. I would like to inform the user correctly about

1-the app being not available

2-the url being not valid

In fact the condition should be !validWebUrl to detect correctly that the url is of a missing app but there are overlapping cases like Apple Maps deep-links, so the best is:

Boolean can = Display.getInstance().canExecute(url);
    if(can != null && can) {
        Display.getInstance().execute(url);
    } else {
        ShowMessage("url not valid or app not available");
    }

Solution

  • This is just a regular expression pattern check you can use the RE class to perform a similar regular expression check but I personally dislike regex and try to go with simpler:

    String lower = url.toLowerCase();
    if(lower.startsWith("http://") || lower.startsWith("https://")) {
    }