Android Volley缓存具有不同的POST请求 [英] Android Volley Caching with different POST requests
问题描述
我正在使用Android Volley来缓存请求,这在我使用GET时可以正常工作,但是出于某些原因,我切换为使用POST.现在,我想使用不同的POST数据来缓存相同的URL.
I am using Android Volley to cache requests this works fine when I was using GET but I switched to use POST for some reasons. Now I want to cache the same URL with different POST data.
- 请求1-> URL1,POST数据="Cat = 1"
- 请求2-> URL1,POST数据="Cat = 2"
- 请求3-> URL1,POST数据="Cat = 3"
这可以通过Android Volley完成吗
is this can be done with Android Volley
推荐答案
,因为Volley.Request.getCacheKey()
返回的URL与我的情况相同;这对我没有用.
as the Volley.Request.getCacheKey()
returns the URL which in my case is the same; this did not work for me.
相反,我不得不在子类中重写getCacheKey()以返回URL + POST(key = Value)
Instead I had to override getCacheKey() in my child class to return URL+POST(key=Value)
这样,我可以使用不同的POST数据缓存对同一URL发出的所有POST请求.
That way I was able to cache all the POST requests made to the same URL with different POST data.
当您尝试检索缓存的请求时,需要以相同的方式构造缓存键.
when you try to retrieve the cached request you need to construct the cache key with the same way.
这是我的代码的快照:
public class CustomPostRequest extends Request<String> {
.
.
private Map<String, String> mParams;
.
.
public void SetPostParam(String strParam, String strValue)
{
mParams.put(strParam, strValue);
}
@Override
public Map<String,String> getParams() {
return mParams;
}
@Override
public String getCacheKey() {
String temp = super.getCacheKey();
for (Map.Entry<String, String> entry : mParams.entrySet())
temp += entry.getKey() + "=" + entry.getValue();// not do another request
return temp;
}
}
无论何时构造新请求,都可以先使用getCacheKey()搜索缓存的请求,然后再将其放入请求队列.
When ever you construct a new request you can use getCacheKey() to search for the cached request first before putting it in the requests queue.
我希望这会有所帮助.
这篇关于Android Volley缓存具有不同的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!