I want to write a android application which needs data from the web. This information is stored in a json-file. The data from the json-file is saved on the device. To keep it up to date, I need to check for changes in the file every hour.
As the remote File can get quite large I want to download it only if it is different from the version which was previously downloaded. I thought about using the Last-Modified-Header of HTTP for this.
I came up with the following workflow (pseudo-code):
data = null; data_timestamp = null;- Every hour repeat:
- Issue a HTTP Head-Request to the URL and option
new_timestampfromLast-ModifiedHeader. - If either
data==nullornew_timestamp > data_timestampthen- Issue a normal HTTP-Request to the URL
- Save to
dataand setdata_timestamp = new_timestamp
- Issue a HTTP Head-Request to the URL and option
Do you think this is a reasonable approach? I could use the if-modified-since HTTP Header to get the data only if it has changed since the last download. This would save me one request. If it has changed, a body containing the new data is provided, if it hasn’t, the body is empty.
I also thought about using ETags, as I typically want to download if the file has new content (and not if the modified-date has changed), but my webserver (nginx) doesn’t support the creation of etags and I don’t want to involve another layer on the server-side for performance-reasons.
I solved the problem as described by me above: Download the file by using the
if-modified-sinceHTTP Header. Thengnixwebserver can be configured to return the right information regarding this header.