Java 如何将字符串响应的值与已安装应用程序的版本进行比较

Java 如何将字符串响应的值与已安装应用程序的版本进行比较,java,token,tokenize,string-parsing,stringtokenizer,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"

我正在尝试使用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>");
但有了这段代码,即使版本是最新的,它也会下载更新

我现在添加了下面的代码来捕获版本号。但是,我不知道该如何将其与我在gradle中的版本进行比较,如果少于,请更新

Pattern p=Pattern.compile(“\n”+
“([0-9.]+)\n”+
"");
匹配器m=p.Matcher(响应);
if(m.find()){
字符串版本=m.group();
//这里有更多代码
}


我是做错了还是需要添加更多代码?我已经读过了,我不能少用。通过这种方式,我还尝试使用“将字符串usin
\n
作为分隔符,隔离中间标记(通过索引-2nd aka 1-或通过使用简单正则表达式解析标记)。 使用类

--编辑:下面是完整的示例--

publicstaticvoidmain(字符串[]args)
{
String response=“blahblah\n”
+“1.1.8”//尝试“1.1.7”、“1.1.9”等,看看这是否有效
+“\nyaddayada”;//这只是为了设置测试:您显然从某个地方得到了响应
字符串myVersion=“1.1.8”;
System.out.println(“我的版本:+myVersion”);
整数versionOK=isVersionOK(响应,myVersion);
字符串s;
if(versionOK!=null)
{
交换机(versionOK)
{
案例0:
s=“相等”;
打破
案例1:
s=“更大”;
打破
违约:
s=“较小”;
}
System.out.println(“版本为”+s);
}
其他的
System.out.println(“无法检测版本!”);
}
/**
*
*@param响应
*@如果相等,则返回0;如果较大,则返回1;如果较小,则返回1;如果未找到,则返回null;或者
*一切都出乎意料-
*/
静态整数isVersionOK(字符串响应,字符串myVersion)
{
字符串WhatToSearchForefore=“\n”;
字符串whattSearchForAfter=“\n”;
if(响应!=null)
{
布尔resp=response.contains(whatSearchForefore)和&response.contains(whatSearchForefter);
System.out.println(“响应是否“+(resp?”:“not”)+“包含版本号”);
如果(resp)
{
String whatWeFound=response.substring(response.indexOf(whatSearchForefore)、response.indexOf(whatSearchForAfter)+whatSearchForAfter.length());
System.out.println(“----n”+whatWeFound+”\n----”;
StringTokenizer st=新的StringTokenizer(whatWeFound,“\n”);
圣奈克特肯();
字符串版本=st.nextToken();
System.out.println(“版本:”+版本);
返回myVersion.compareToIgnoreCase(版本);
}
}
返回null;
}

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

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(“\n”+
“([0-9.]+)\n”+
"");
匹配器m=p.Matcher(响应);
if(m.find()){
字符串版本=m.group();
//这里有更多代码
}
其中
([0-9.]+)
捕获数字(
0-9
)和句点(
)的任意组合

现在,您只需要了解如何检查
版本
是否小于
1.1.8
。您不能使用字符串比较,因为
“1.1.10”
“1.1.8”
,即使它是更高的“版本”。

请参见

字符串比较是否在此处-->//更多代码here@MarkHarrop是的,除了使用版本比较,而不是简单的字符串比较。如果您稍等,我将在下午晚些时候(即几小时后)为您编写实际代码我现在就要试试这个,会回来的谢谢@unaiI编辑了我的答案,包括完整的工作代码,你可以简单地复制和粘贴,准备继续。看看吧,兄弟!我希望这是你想要的被接受的答案
 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
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;
}
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
}