java - 我如何比较字符串响应的值与已安装应用程序的版本

标签 java token tokenize string-parsing stringtokenizer

我正在尝试使用 Volley 库和 Dropbox API 编写自动更新。我已将其设置为读取博客页面并找到这段代码:

if (response != null) {
            boolean resp = response.contains("<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n" +
                    "1.1.8\n" +
                    "<div style='clear: both;'></div>");

使用此代码,我可以说如果响应小于 1.1.8,则下载更新。我尝试使用这种方法这样做:

  if (!resp){My Intent code is here } 

但使用此代码,即使版本是最新的,它也会下载更新。

我现在添加了下面的代码来捕获版本号。但我不知道如何将它与我的 gradle 中的任何一个版本进行比较,并说如果小于请更新

 Pattern p = Pattern.compile("<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n" +
            "([0-9.]+)\n" +
            "<div style='clear: both;'></div>");

Matcher m = p.matcher(response);
 if (m.find()) {
String version = m.group();
// more code here

我是做错了什么还是需要添加更多代码?我读过我不能用少于它。这样我也尝试过使用“<=”比较。如果您认为可能是我的其他代码导致了问题,请告诉我,我可以发布更多内容供您查看。

最佳答案

使用正则表达式获取版本号:

Pattern p = Pattern.compile("<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n" +
                "([0-9.]+)\n" +
                "<div style='clear: both;'></div>");
Matcher m = p.matcher(response);
if (m.find()) {
    String version = m.group();
    // more code here
}

其中 ([0-9.]+) 捕获数字 (0-9) 和句点 (.) 的任意组合。

现在您只需要弄清楚如何检查 version 是否小于 1.1.8。您不能为此使用字符串比较,因为 "1.1.10" 比较小于 "1.1.8",即使它是更高的“版本”。
参见 How do you compare two version Strings in Java?

关于java - 我如何比较字符串响应的值与已安装应用程序的版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947818/

相关文章:

string - 如何从命令行字符串中获取类似 os.Args 的标记

xQuery 中用于标记化的正则表达式

java - 如何在java中的另一个类中使用嵌套类?

java - Guice:无法在请求范围内注入(inject)带注释的类型

asp.net - 使用 WIF 安全 token 服务将用户的 asp.net session 限制为 1

java - 在 UsernamePasswordAuthenticationFilter 或 Controller 中创建 JWT token ?

parsing - 术语提取: Generatings tags out of text

regex - 在 Go 中获取正则表达式的解析树

java - 无法在目录中找到 feature.xml

java - 根据用户输入点创建折线图