android - 检查 Volley 是从缓存还是通过网络获取结果

标签 android caching android-volley

如何检查 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/

相关文章:

安卓 Volley : Cannot resolve Constructor jsonobjectrequest

java.lang.NoClassDefFoundError : android. support.v7.internal.widget.TintManager

android - 在视频结束后结束 Activity

caching - 防止浏览器缓存 UI5 应用程序资源

Android Volley 库不适用于 204 和空体响应

android - 无法解析符号 newRequestQueue - Volley

android - 如何删除我的编辑文本上的 ` frequently used email ` toast?

android - Retrofit 2.0方法无响应

caching - 处理 Redis 缓存存储断开连接上的 Nest.js 应用程序

mysql - ORDER BY 中使用的计算值是否在排序期间重新计算过?