I'm trying to simulate a process like "cache validation" in my application. I will download a new version of my webapplication on the device (android-based), but I only want to download a new version of the files, based in a etag comparision. Does anyone a example of how use the Etag mechanism in Android?
Asked
Active
Viewed 3,156 times
4 Answers
1
You can access the ETag field from a HttpURLConnection object such as like this:
HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
String etag = conn.getHeaderField("ETag");
Of course, you will need to make sure that the server you are testing this against supports ETags.

k3v
- 1,189
- 12
- 12
0
Maybe class "HttpRequest" from this library (kevinsawicki) will help you.
For example:
File latest = new File("/data/cache.json");
HttpRequest request = HttpRequest.get("http://google.com");
//Copy response to file
request.body(latest);
//Store eTag of response
String eTag = request.eTag();
//Later you can check if changes exist
boolean unchanged = HttpRequest.get("http://google.com")
.ifNoneMatch(eTag)
.notModified();

valerybodak
- 4,195
- 2
- 42
- 53
0
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
HttpResponse response = httpClient.execute(httpPost);
Log.d("Http Response:", response.getFirstHeader("etag").toString());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Omid Aminiva
- 667
- 5
- 14
0
You can refer to the specific implement of ShallowEtagHeaderFilter performing etag generation and validation in Spring.

Sunny
- 181
- 1
- 7