Update
You could use the Expires header for cache control so you can avoid unneeded downloads. I don't think it´s a good approach but in this case since you don't have control over the server side, it´s the only way I could think of right now.
The expiration time of an entity MAY be specified by the origin server
using the Expires header (see section 14.21). Alternatively, it MAY be
specified using the max-age directive in a response. When the max-age
cache-control directive is present in a cached response, the response
is stale if its current age is greater than the age value given (in
seconds) at the time of a new request for that resource. The max-age
directive on a response implies that the response is cacheable (i.e.,
"public") unless some other, more restrictive cache directive is also
present.
If a response includes both an Expires header and a max-age directive,
the max-age directive overrides the Expires header, even if the
Expires header is more restrictive. This rule allows an origin server
to provide, for a given response, a longer expiration time to an
HTTP/1.1 (or later) cache than to an HTTP/1.0 cache. This might be
useful if certain HTTP/1.0 caches improperly calculate ages or
expiration times, perhaps due to desynchronized clocks.
Many HTTP/1.0 cache implementations will treat an Expires value that
is less than or equal to the response Date value as being equivalent
to the Cache-Control response directive "no-cache". If an HTTP/1.1
cache receives such a response, and the response does not include a
Cache-Control header field, it SHOULD consider the response to be
non-cacheable in order to retain compatibility with HTTP/1.0 servers.
Note: An origin server might wish to use a relatively new HTTP cache
control feature, such as the "private" directive, on a network
including older caches that do not understand that feature. The origin
server will need to combine the new feature with an Expires field
whose value is less than or equal to the Date value. This will prevent
older caches from improperly caching the response.
There're different approaches. I use this one:
- On the server response we get the Etag header and save it on SharedPreferences.
- Every server call goes with the "If-None-Match" header with the Etag value.
- The server, compares the Etag values and returns 304 - Not Modified or the result of the request itself if something changed and the content needs to be updated.
You can use a RequestInterceptor
to do this as you pointed out:
public class HeaderRequestInterceptor implements RequestInterceptor {
private final static String TAG =
HeaderRequestInterceptor.class.getSimpleName();
private SharedPreferences mPreferences;
public HeaderRequestInterceptor() {
mPreferences = PreferenceManager.getDefaultSharedPreferences(
DaoApplication.getAppContext());
}
@Override
public void intercept(RequestFacade request) {
String etagValue = mPreferences.getString(EtagConfig.MY_ETAG_VALUE, "");
request.addHeader("If-None-Match", etagValue);
}
}
Sample output:
Retrofit D ---> HTTP GET https://url.irontec.com/rest/schedule
D If-None-Match:
D Authorization: MyToken M2JiOGQwZGNjNWJiNWNiOTA1Yjc3YTA0YTAyMzEwYWY6OjIwMTUtMTAtMDhUMTM6MDc6MDMrMDA6MDA=
D Connection: close
Retrofit D <--- HTTP 200 https://url.irontec.com/rest/schedule (559ms)
D : HTTP/1.1 200 OK
D Access-Control-Allow-Credentials: true
D Access-Control-Allow-Headers: Authorization, Origin, Content-Type, X-CSRF-Token
D Access-Control-Allow-Methods: GET, PUT, POST, OPTIONS, DELETE
D Access-Control-Allow-Origin: *
D Connection: close
D Content-Type: application/json; charset=UTF-8;
D Date: Thu, 08 Oct 2015 13:07:07 GMT
D Etag: a3145c3f85f2dca1c78f87107331c766
D Server: Apache
D Transfer-Encoding: chunked
D X-Android-Received-Millis: 1444309624169
D X-Android-Response-Source: NETWORK 200
D X-Android-Sent-Millis: 1444309623870
D X-Content-Type-Options: nosniff
D X-Frame-Options: sameorigin
Now when refreshing the content:
Retrofit D ---> HTTP GET https://url.irontec.com/rest/schedule
D If-None-Match: a3145c3f85f2dca1c78f87107331c766
D Authorization: MyToken MGQ1OWM4YjViYTMxZWM3OGRmMDBlYTZjNmFjNDY3MmI6OjIwMTUtMTAtMDhUMTM6MTA6MDkrMDA6MDA=
D Connection: close
D ---> END HTTP (no body)
Retrofit D <--- HTTP 304 https://url.irontec.com/rest/schedule (299ms)
D : HTTP/1.1 304 Not Modified
D Connection: close
D Date: Thu, 08 Oct 2015 13:10:12 GMT
D Server: Apache
D X-Android-Received-Millis: 1444309809335
D X-Android-Response-Source: NETWORK 304
D X-Android-Sent-Millis: 1444309809163
D <--- END HTTP (0-byte body)