我如何比较字符串响应的值与已安装应用程序的版本
How can i compare the value of a string response against version of installed app
我正在尝试使用 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
}
我是做错了什么还是需要添加更多代码?我读过我不能用少于它。通过这种方式,我也尝试过使用“<=”比较。如果您认为可能是我的其他代码引起了问题,请告诉我,我可以 post 更多内容供您查看。
使用 \n
作为分隔符来标记该字符串,隔离中间标记(通过索引 - 2nd aka 1 - 或通过使用简单的正则表达式解析标记)。
使用 this class.
--编辑:这是完整的例子--
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
}
其中 ([0-9.]+)
捕获数字 (0-9
) 和句点 (.
) 的任意组合。
现在您只需要弄清楚如何检查 version
是否小于 1.1.8
。您不能为此使用字符串比较,因为 "1.1.10"
比较小于 "1.1.8"
,即使它更高 "version".
参见 How do you compare two version Strings in Java?
我正在尝试使用 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
}
我是做错了什么还是需要添加更多代码?我读过我不能用少于它。通过这种方式,我也尝试过使用“<=”比较。如果您认为可能是我的其他代码引起了问题,请告诉我,我可以 post 更多内容供您查看。
使用 \n
作为分隔符来标记该字符串,隔离中间标记(通过索引 - 2nd aka 1 - 或通过使用简单的正则表达式解析标记)。
使用 this class.
--编辑:这是完整的例子--
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
}
其中 ([0-9.]+)
捕获数字 (0-9
) 和句点 (.
) 的任意组合。
现在您只需要弄清楚如何检查 version
是否小于 1.1.8
。您不能为此使用字符串比较,因为 "1.1.10"
比较小于 "1.1.8"
,即使它更高 "version".
参见 How do you compare two version Strings in Java?