0

I have an XML file that I am parsing in my Android app. I would like to NOT parse it if the file has not been changed.

I can get the time when my file was last modifed and I can get the last-modified date from the server header in GMT, but am not sure what to do now...

    //this is a string: status.getHeader("last-modified"); 
    //Shows like so: Tue, 05 March 2013 16:45:02 GMT
    String serverlastmodified = status.getHeader("last-modified");    
    Log.e("Header",serverlastmodified);

    Date s = status.getTime();
    String filelastgotten = s.toGMTString();
    //String shows like so: 5 Mar 2013 19:41:43 GMT
    Log.e("LM", filelastgotten);

//this needs to be a great-than / less-than? 
//But I cannot do that to a string... so change to int?
    if(serverlastmodified.equals(filelastgotten)){
        Log.e(TAG,"YES!");
    }else{
        Log.e(TAG,"NO!");
    }

EDIT:::::::::

How does this look?

I ended up just making sure the two dates where not the same using SimpleDateFormat and toGMTString();

  String serverLastModified = status.getHeader("last-modified");
                SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyy HH:mm:ss zzz");

                Date d = null;
                try {
                    d = sdf.parse(serverLastModified);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Date fileLastGotten = status.getTime();

                Log.e("DATE1", d.toGMTString());
                Log.e("DATE2", fileLastGotten.toGMTString());

                if (d.before(fileLastGotten)) {
                    Log.e("DS", "YES!");
                } else {
                    Log.e("DS", "NO!");
                }
jasonflaherty
  • 1,924
  • 7
  • 40
  • 82

3 Answers3

1

The native app should keep an MD5 hash of the file you wish to parse. Before you request the file, request its MD5 from the server. If this MD5 matches your local MD5 than don't request the file. Otherwise do and parse it. Don't forget to update the native MD5 if a new file is parsed.

atreat
  • 4,243
  • 1
  • 30
  • 34
  • Hi atreat, thanks for the comment. So the Server XML file should have an MD5 associated with it and then my app would decode this and compare? I am not sure that the XML file has this. If I try status.getHeader("Content-MD5"); I get null... so I suppose not. Any thoughts? – jasonflaherty Mar 05 '13 at 22:17
  • An MD5 is not something the file would contain. if you go to a UNIX command line, you can type "md5 " to see the MD5 of the file. Basically what it does is crunch the bits of your file into a unique string. If the file changes, the MD5 will also change. I would read up on the MD5 hashing algorithm and other hashing algorithms. They can be very useful. – atreat Mar 05 '13 at 22:23
  • Yes, so is that something that would need to be created at the server level? Then I can use getHeader() to get it? – jasonflaherty Mar 05 '13 at 22:35
1

I just completed an open source library called droidQuery that can handle this task for you by returning an error, instead of a success, if the data has not been modified:

$.ajax(new AjaxOptions(url).type("GET")
                           .dataType("XML")
                           .ifModified(true)
                           .context(this)
                           .success(new Function() {
                               @Override
                               public void invoke($ droidQuery, Object... params) {
                                   Document xml = (Document) params[0];
                                   //TODO parse XML Document
                               }
                           })
                           .error(new Function() {
                               @Override
                               public void invoke($ droidQuery, Object... params) {
                                   int status = (Integer) params[1];
                                   if (status == 304) {
                                       Log.i("ajax", "data was not modified. No need to parse");
                                   }
                                   else {
                                       Log.e("ajax", "error: " + (String) params[2];
                                   }
                               }
                           }));
Phil
  • 35,852
  • 23
  • 123
  • 164
0

You should parese it with

try {
    long serverTime = Date.parse(serverLastModified).getTime();
    long myTime = status.getTime.getTime();
    if (serverTime != myTime) {
        //Not equals
    } else {
        //equals
    }
} catch (Exception e) {
    //can't parse server string
}
pavko_a
  • 507
  • 4
  • 16
  • Does that just check if my current time is different than the last time I parsed the file? – jasonflaherty Mar 05 '13 at 21:43
  • @buildakicker, this is check for equality. If you replace `!=` whith `>` then it will mean that serverTime is after your time and you should update your file – pavko_a Mar 06 '13 at 10:26