Android 告诉Volley不要使用缓存数据,而是启动新请求?

Android 告诉Volley不要使用缓存数据,而是启动新请求?,android,caching,android-volley,Android,Caching,Android Volley,我在应用程序中遇到了一个问题,我认为它可能与从缓存中截取数据有关 也就是说,应用程序与API紧密绑定,因此每个更改都会发送到API,然后使用Volley库从API中检索。因此,用户将打开一个弹出窗口,选择某个组查看其项目,然后选择某个值将其标记为收藏夹。弹出窗口将关闭,片段将重新加载新数据 当用户再次打开弹出窗口,选择同一组加载其数据时,以前的项目将不会显示为收藏夹。但当用户再次触摸同一组以重新加载其数据时,该项目将显示为收藏夹 我已经一步一步地调试了代码,没有发现任何错误。因此,我得出结论,V

我在应用程序中遇到了一个问题,我认为它可能与从缓存中截取数据有关

也就是说,应用程序与API紧密绑定,因此每个更改都会发送到API,然后使用Volley库从API中检索。因此,用户将打开一个弹出窗口,选择某个组查看其项目,然后选择某个值将其标记为收藏夹。弹出窗口将关闭,片段将重新加载新数据

当用户再次打开弹出窗口,选择同一组加载其数据时,以前的项目将不会显示为收藏夹。但当用户再次触摸同一组以重新加载其数据时,该项目将显示为收藏夹

我已经一步一步地调试了代码,没有发现任何错误。因此,我得出结论,Volley可能正在从其缓存中提取数据,同时在我第二次单击该组时启动一个新的API请求

我想测试它是否是缓存问题,或者我必须进行更深入的调试

有没有办法告诉Volley不要使用缓存的请求数据,而是向API发起新的请求?例如,不要使用缓存数据,而是发出新请求


注意:我不想删除完整的缓存。我只想告诉Volley什么时候启动对API的全新请求

甚至我以前也遇到过同样的问题,但是你可以用这个来改变它

request.setShouldCache(false);
myQueue.add(request);

在我看来,如果您的项目使用Google的volley作为模块而不是jar文件,那么您可以自定义它的类,如下所示:

        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache() || !mCacheUsed) {
            mNetworkQueue.add(request);
            return request;
        }
        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache() || request.mSkipAvailableCache) {
            mNetworkQueue.add(request);
            return request;
        }
备选案文1:

第一个文件RequestQueue.java:

添加一个类变量private boolean mCacheUsed=true

以及以下施工人员:

    public RequestQueue(Cache cache, Network network, int threadPoolSize,
                        ResponseDelivery delivery, boolean cacheUsed) {
        mCache = cache;
        mNetwork = network;
        mDispatchers = new NetworkDispatcher[threadPoolSize];
        mDelivery = delivery;
        mCacheUsed = cacheUsed;
    }

    public RequestQueue(Cache cache, Network network, int threadPoolSize, boolean cacheUsed) {
        this(cache, network, threadPoolSize,
                    new ExecutorDelivery(new Handler(Looper.getMainLooper())), cacheUsed);
    }

    public RequestQueue(Cache cache, Network network, boolean cacheUsed) {
        this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE, cacheUsed);
    }
然后,在public Request addRequest Request{中,检查如下:

        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache() || !mCacheUsed) {
            mNetworkQueue.add(request);
            return request;
        }
        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache() || request.mSkipAvailableCache) {
            mNetworkQueue.add(request);
            return request;
        }
第二个文件,Volley.java:

最后,在MainActivity中,例如:

如果要使用可用缓存:

RequestQueue requestQueue = Volley.newRequestQueue(this, true); 
RequestQueue requestQueue = Volley.newRequestQueue(this, false); 
如果不想使用可用缓存:

RequestQueue requestQueue = Volley.newRequestQueue(this, true); 
RequestQueue requestQueue = Volley.newRequestQueue(this, false); 
备选案文2:

Request.java:

添加一个类变量public boolean mSkipAvailableCache=false

RequestQueue.java:

在public Request addRequest请求中,您检查如下:

        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache() || !mCacheUsed) {
            mNetworkQueue.add(request);
            return request;
        }
        // If the request is uncacheable, skip the cache queue and go straight to the network.
        if (!request.shouldCache() || request.mSkipAvailableCache) {
            mNetworkQueue.add(request);
            return request;
        }
MainActivity.java: 你可以设置

jsonArrayRequest.mSkipAvailableCache = true;
将不使用可用缓存。
希望这有帮助!

那么你没有找到没有setShouldCachebool的其他方法了吗?我有一个通用的Volley类,所以所有片段都使用相同的add-request方法。这样的话,我必须严格更改逻辑200多个位置。哦,这是一个大问题,但我认为你不能没有它,因为你无法编辑Volley库。酷!我会检查的谢谢。你的观点很好,但我的截击是从Gradle中提取的。所以我想要么切换到本地模块,要么为非缓存请求创建另一个方法。这项任务还没有进行,可能在下周。