Android: how to find out the file located on the web server has been changed?

I parse the Xml file located on the web server and save the analyzed data in the database. for my application, I use data from the database. I need to parse the XML file only if the file is modified, otherwise there is no need to parse. So how can I find out that the file has been modified? I know that I can use the if-modified-since header. But I need some example if-modified-since headers please help me .......

+7
source share
2 answers

Since you are extracting an XML file from a web server, this should be relatively easy without having to do the MD5 sum on the server side.

If you are making an HTTP request for an xml file, you can simply execute the HEAD request from the web server, and it will return if the file has changed or changed, or if it does not exist. It is also light weight, and the best part is that the server should already do this for you.

Edit : re-read your question, it looks like you had the same idea. Here is the code.

import java.net.*; import java.io.*; // Using HTTP_NOT_MODIFIED public static boolean Changed(String url){ try { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED); } catch (Exception e) { e.printStackTrace(); return false; } } // GET THE LAST MODIFIED TIME public static long LastModified(String url) { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); long date = con.getLastModified(); if (date == 0) System.out.println("No last-modified information."); else System.out.println("Last-Modified: " + new Date(date)); return date; } 

See:

Alternatively, if your server supports them, you can use ETags to find out if your file has been modified.

+16
source

Calculate MD5 file. Can you keep the old one and compare it?

If you do not know how to check this, for example: Getting MD5 checksum in Java

+1
source

All Articles