1

I'm trying to write a auto update using Volley library and Dropbox API. I've set it up so that it reads a blog page and finds this chunk of code:

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>");

Using this code I can say if response is less than 1.1.8 then download the update. I tried to do so using this approach:

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

but with this code it downloads the update even if the version is up to date.

I've now added the code below to capture the version number. but I cant seam to get my head round how to compare that against either my version in my gradle and to say if less than please update.

 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

}

Am I doing something wrong or do I need to add more code? I've read I can't use less than it. In this way I have also tried using "<=" comparison. If you think it may be some other piece of my code causing the problem let me know and I can post more for you to see.

Unai Vivi
  • 2,893
  • 3
  • 25
  • 44
Mark Harrop
  • 158
  • 10
  • I edited my answer including the full working code that you can simply copy and paste to be ready to go on. Check it out, bro'! I hope that it'll be the accepted answer you were looking for – Unai Vivi Aug 15 '16 at 18:52

2 Answers2

2

Capture the version number using a regular expression:

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
}

where ([0-9.]+) captures any combination of digits (0-9) and periods (.).

Now you just need to figure out how to check if version is less than 1.1.8. You can't use string comparison for this, since "1.1.10" compares less than "1.1.8", even though it's a higher "version".
See How do you compare two version Strings in Java?

Community
  • 1
  • 1
Andreas
  • 138,167
  • 8
  • 112
  • 195
1

Tokeinze that string usin \n as a delimiter, isolate the middle token (either by index - 2nd aka 1 - or by parsing tokens with a simple regex). Use this class.

--EDIT: Here's the full example--

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 Vivi
  • 2,893
  • 3
  • 25
  • 44