问题描述
我想让凌空缓存从服务器获得的响应.因此,我实现了缓存代码(ParseNetworkResponse).但是当发出POST请求时,凌空便会发送缓存的参数.如何防止凌空抽烟呢?最好如何修改下面的缓存代码以停止缓存POST参数?我只希望缓存来自服务器的响应.
I want volley to cache the response obtained from the server. Hence, I have implemented the caching code (ParseNetworkResponse). But volley is sending cached parameters when a POST request is made. How to prevent volley from doing this? Preferably how should the caching code below be modified to stop caching POST parameters? I want only the response from the server to be cached.
public NetworkConnector(final Context ctx, String url, String methodType, final ArrayList<ArrayList<String>> postData,
final RequestCompleteListener<String> listener) {
//postData has new data whenever class called
if (methodType.equals("POST")) {
method = Request.Method.POST;
} else if (methodType.equals("GET")) {
method = Request.Method.GET;
}
VolleySingleton volleySingleton = VolleySingleton.getInstance();
requestQueue = volleySingleton.getRequestQueue();
StringRequest stringRequest = new StringRequest(method, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
listener.onRequestExecuted("response", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("CCC", "Error " + error.toString());
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
for (int i = 0; i < postData.size(); i++) {
params.put(postData.get(i).get(0), postData.get(i).get(1));
//postData sends old data here
//so a hashmap is consists of old values
}
return params;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
try {
Cache.Entry cacheEntry = HttpHeaderParser.parseCacheHeaders(response);
if (cacheEntry == null) {
cacheEntry = new Cache.Entry();
}
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
long now = System.currentTimeMillis();
final long softExpire = now + cacheHitButRefreshed;
final long ttl = now + cacheExpired;
cacheEntry.data = response.data;
cacheEntry.softTtl = softExpire;
cacheEntry.ttl = ttl;
String headerValue;
headerValue = response.headers.get("Date");
if (headerValue != null) {
cacheEntry.serverDate = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
headerValue = response.headers.get("Last-Modified");
if (headerValue != null) {
cacheEntry.lastModified = HttpHeaderParser.parseDateAsEpoch(headerValue);
}
cacheEntry.responseHeaders = response.headers;
final String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(jsonString, cacheEntry);
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(String response) {
super.deliverResponse(response);
}
@Override
public void deliverError(VolleyError error) {
super.deliverError(error);
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
return super.parseNetworkError(volleyError);
}
};
requestQueue.add(stringRequest);
}
推荐答案
这不是解决问题的方法,而是一种解决方法:
This is not a solution to the problem but a workaround:
currentURL = url;
if (methodType.equals("POST")) {
currentURL = currentURL + "?timestamp=" + String.valueOf(System.currentTimeMillis());
}
这里是一个时间戳(timestamp)的虚拟参数正在发送到网站.该网站不使用此参数,因此,它没有任何改变.每次执行发布请求时,它仅提供一个唯一的凌空URL.在缓存存储期过后(即本例中的24小时),带有时间戳的url缓存数据将被删除,因此缓存不会建立.
Here a dummy parameter which is timestamp is being sent to the website. The website does not use this parameter and hence, it changes nothing. It just provides a unique url to volley every time a post request is executed. After the period of cache storage, i.e. 24hrs in my case, the cached data for the url with timestamp is deleted hence, cache doesn't buildup.
注意:不建议使用此替代方法,因为发送或接收大量数据并且由于高速缓存可能已满而向该URL进行大量发布请求的URL.
Note: this workaround is not recommended for url where large amount of data is sent or received and large volume of post request to the url are made as the cache may get full.
这篇关于当在Volley中实现缓存时,防止Volley在POST请求方法上发送缓存的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!