Volley deshabilitar caché para una solicitud
Quiero usar la política de caché predeterminada de volley para todas mis solicitudes, excepto una . es posible?
Me gustaría obtener la respuesta de Internet cada vez que se llama a esta solicitud.
Gracias por adelantado !
Puede deshabilitar fácilmente la caché de una solicitud en particular cambiando el método com.android.volley.toolbox.HttpHeaderParser.parseCacheHeaders(NetworkResponse response)
e ignorar estos encabezados, establecer los campos entry.softTtl
y entry.ttl
a cualquier valor que funcione para usted y utilizar su método en su clase de solicitud. Aquí hay un ejemplo:
public static Cache.Entry parseIgnoreCacheHeaders(NetworkResponse response) { long now = System.currentTimeMillis(); Map<String, String> headers = response.headers; long serverDate = 0; String serverEtag = null; String headerValue; headerValue = headers.get("Date"); if (headerValue != null) { serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue); } serverEtag = headers.get("ETag"); final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely final long softExpire = now + cacheHitButRefreshed; final long ttl = now + cacheExpired; Cache.Entry entry = new Cache.Entry(); entry.data = response.data; entry.etag = serverEtag; entry.softTtl = softExpire; entry.ttl = ttl; entry.serverDate = serverDate; entry.responseHeaders = headers; return entry; }
Utilice este método en su respuesta como esta
public class MyRequest extends com.android.volley.Request<MyResponse> { ... @Override protected Response<MyResponse> parseNetworkResponse(NetworkResponse response) { String jsonString = new String(response.data); MyResponse MyResponse = gson.fromJson(jsonString, MyResponse.class); return Response.success(MyResponse, HttpHeaderParser.parseIgnoreCacheHeaders(response)); } }
Esto funcionaría:
request.setShouldCache(false);