我问了一个question关于如何在昨天更新Android中的Volley缓存并且没有人回答它,我找不到任何关于在Volley中更新缓存的文档。我想知道是否可能误解了缓存的更新方式。
如何更新Volley缓存?
修改
只是为了澄清,我带下面的代码来解释我的问题:
private void checkCache(String service_address,
final int termId,
int node_Id) {
Cache cache = MyApplication.getInstance().getRequestQueue(true).getCache();
Cache.Entry entry = cache.get(service_address);
/*
cache is full- maybe new maybe old
*/
if (entry != null) {
if(networkChecker.isConnected()) {
Calendar calendar = Calendar.getInstance();
long serverDate = MyApplication.getInstance().getRequestQueue(false).getCache().get(service_address).serverDate;
if (Utility.getMinutesDifference(serverDate, calendar.getTimeInMillis()) >= 30) {
MyApplication.getInstance().getRequestQueue(false).getCache().invalidate(service_address, true);
//does this code referesh cache?how to referesh cache for new incomming data
}
}
ShowOrUpdateBadger();
pDialog.hide();
}
/*
//cache is empty---- should get all news
*/
else {
/*
can not fetch data-->close app
*/
if (!networkChecker.isConnected()) {
pDialog.cancel();
Utility.ShowFaildMessage(MainActivity.this);
}
/*
load from internet
*/
else {
GetonlineNewGalleryData(service_address, termId, true, node_Id);
}
}
}
我在onCreate()
中MainActivity
的应用程序中打开上述方法。问题是,在下面的代码中,我如何用新消息更新缓存?
if (entry != null) {
...
}
EDITE :
我更新了我的代码,我的代码如下:
private void checkCache(String service_address,
final int termId,
int node_Id) {
Cache cache = MyApplication.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(service_address);
if(networkChecker.isConnected())
{
if(entry!=null)
{
MyApplication.getInstance().getRequestQueue().getCache().invalidate(service_address,true);
MyApplication.getInstance().getRequestQueue().getCache().remove(service_address);
}
GetonlineNewGalleryData(service_address, termId, true, node_Id);
}
else
{
if(entry==null)
{
pDialog.cancel();
Utility.ShowFaildMessage(MainActivity.this);
}
else
{
ShowOrUpdateBadger();
pDialog.hide();
}
}
现在问题是,我的缓存数据是400新闻,新数据是2更新后做更新缓存我的缓存号是802新闻!