如何检查 Volley 是从缓存中还是从网络中获取 JsonObjectRequest 的结果?
我需要在需要网络连接时显示进度对话框,但在从缓存中快速接收结果时不需要。
我的请求看起来像这样
volleyQueue = Volley.newRequestQueue(this);
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>(){...stuff}, new Response.ErrorListener(){...errorstuff});
jr.setShouldCache(true);
volleyQueue.add(jr);
最佳答案
我通过覆盖 Request#addMarker 并检查是否添加了“缓存命中”标记来做到这一点:
public class MyRequest<T> extends Request<T> {
protected boolean cacheHit;
@Override
public void addMarker(String tag) {
super.addMarker(tag);
cacheHit = false;
if (tag.equals("cache-hit")){
cacheHit = true;
}
}
}
关于android - 检查 Volley 是从缓存还是通过网络获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19972548/