Skip to content

Instantly share code, notes, and snippets.

@yrom
Created October 24, 2013 06:49
Show Gist options
  • Save yrom/7132438 to your computer and use it in GitHub Desktop.
Save yrom/7132438 to your computer and use it in GitHub Desktop.
Volley: 强制缓存
...
protected Response<?> parseNetworkResponse(NetworkResponse response) {
try {
String json = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
... // parseing json
return Response.success(obj, cache(response, 60));
} catch (Exception e) {
Log.e(TAG, "parse article error", e);
return Response.error(new ParseError(e));
}
}
...
public static Cache.Entry cache(NetworkResponse response, long maxAge){
long now = System.currentTimeMillis();
if(maxAge == 0) maxAge = 60;
Map<String, String> headers = response.headers;
long serverDate = 0;
long softExpire = 0;
String serverEtag = null;
String headerValue;
headerValue = headers.get("Date");
if (headerValue != null) {
serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
softExpire = now + maxAge * 1000;
Cache.Entry entry = new Cache.Entry();
entry.data = response.data;
entry.etag = serverEtag;
entry.softTtl = softExpire;
entry.ttl = entry.softTtl;
entry.serverDate = serverDate;
entry.responseHeaders = headers;
return entry;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment