简体   繁体   English

Url 代码名称一中的模式匹配用于验证 url

[英]Url pattern matching in Codename One for validating urls

My Android app performs some checks on urls when opening fails.我的 Android 应用程序在打开失败时对 url 执行一些检查。

Now I am trying to port it to iOS by Codename One.现在我正在尝试将其移植到代号 One 的 iOS。

My code is below.我的代码如下。 Note that Android code is inserted, I would like to have the Codename counterpart:请注意,插入了 Android 代码,我想要对应的代号:

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 1-应用程序不可用

2-the url being not valid 2-url 无效

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:实际上,条件应该是,validWebUrl 以正确检测 url 是缺少应用程序,但有重叠的情况,如 Apple Maps 深层链接:所以最好的是:

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

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:这只是一个正则表达式模式检查,您可以使用 RE class 执行类似的正则表达式检查,但我个人不喜欢正则表达式并尝试使用更简单的 go:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM