【问题标题】:How can i compare the value of a string response against version of installed app如何将字符串响应的值与已安装应用程序的版本进行比较
【发布时间】:2016-08-15 00:23:35
【问题描述】:

我正在尝试使用 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

}

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

【问题讨论】:

  • 我编辑了我的答案,包括完整的工作代码,您只需复制和粘贴即可继续。看看吧,兄弟!我希望这将是您正在寻找的公认答案

标签: java token tokenize string-parsing stringtokenizer


【解决方案1】:

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

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?

【讨论】:

  • 字符串比较会放在这里----> //更多代码在这里
  • @MarkHarrop 是的,除了使用 version 比较,而不是纯字符串比较。
【解决方案2】:

使用\n 作为分隔符来标记该字符串,隔离中间标记(通过索引 - 2nd aka 1 - 或通过使用简单的正则表达式解析标记)。 使用this 类。

--编辑:这是完整的示例--

public static void main(String[] args)
{
    String response = "blahblah<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n"
            + "1.1.8"//try "1.1.7", "1.1.9", etc to see that this does work
            + "\n<div style='clear: both;'></div>yaddayadda";//this is just to setup the test: you're obviously getting response from somewhere
    String myVersion = "1.1.8";
    System.out.println("My version: "+myVersion);
    Integer versionOK = isVersionOK(response, myVersion);
    String s;
    if (versionOK != null)
    {
        switch (versionOK)
        {
            case 0:
                s = "equal";
                break;
            case -1:
                s = "greater";
                break;
            default:
                s = "lesser";
        }
        System.out.println("Version is " + s);
    }
    else
        System.out.println("Couldn't detect version!");
}

/**
 *
 * @param response
 * @return 0 if equal, -1 if greater, 1 if lesser, null if not found or
 * anything goes unexpectedly-
 */
static Integer isVersionOK(String response, String myVersion)
{
    String whatToSearchForBefore = "<div class='post-body entry-content' id='post-body-6791062644900393367' itemprop='description articleBody'>\n";
    String whatToSearchForAfter = "\n<div style='clear: both;'></div>";
    if (response != null)
    {
        boolean resp = response.contains(whatToSearchForBefore)&&response.contains(whatToSearchForAfter);
        System.out.println("response does " + (resp ? "" : "not ") + "contain the version number");
        if (resp)
        {
            String whatWeFound = response.substring(response.indexOf(whatToSearchForBefore), response.indexOf(whatToSearchForAfter) + whatToSearchForAfter.length());
            System.out.println("----\n"+whatWeFound+"\n----");
            StringTokenizer st = new StringTokenizer(whatWeFound, "\n");
            st.nextToken();
            String version = st.nextToken();
            System.out.println("Version: " + version);
            return myVersion.compareToIgnoreCase(version);
        }
    }
    return null;
}

【讨论】:

  • 如果你坚持下去,我会在 [意大利] 下午晚些时候(即几个小时内)为你编写实际代码
  • 我现在要试试这个会报告谢谢@unai
猜你喜欢
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 2020-08-21
  • 1970-01-01
  • 1970-01-01
  • 2022-10-19
  • 2021-03-29
  • 1970-01-01
相关资源
最近更新 更多