告诉Volley不要使用缓存的数据,而是要发起新请求? [英] Tell Volley not to use cached data but to initiate a new request?

查看:115
本文介绍了告诉Volley不要使用缓存的数据,而是要发起新请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中遇到一个问题,我认为它可能与Volley关联,该Volley从缓存中提取数据.

I am having an issue in the app which I think it may be connected to Volley pulling data from the cache.

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

Namely, the app is heavily bound to the API so each change is being sent to the API and then later retrieved from the API using the Volley library. So a user will open a popup, chose some group to see its items then select some value to mark it favorite. The popup will close and the fragment will reload with new data.

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

When a user opens the popup again, selects the same group to load its data, the previously item will not be shown as favorite. But when a user touched the same group once more to reload its data, the item WILL BE shown as favorite.

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

I have debugged the code step by step and I found no error. So I concluded that Volley may be pulling data from its cache, while initiating a new API request the 2nd time I click on the group.

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

I would like to test if it's a cache issue or I have to debug deeper.

是否有一种方法告诉Volley不要使用缓存的请求数据,而是向API发起新请求? don't use cached data, but make a new request之类的东西.

Is there a way to tell Volley NOT to use cached request data, but to initiate the new request to API? Something like don't use cached data, but make a new request.

注意:我不想删除整个缓存.我只想告诉Volley何时向API发起一个全新的请求.

NOTE: I would not like to delete the complete cache. I'd only like to tell Volley when to initiate a brand new request to the API.

推荐答案

IMO,如果您的项目使用Google的凌空作为模块(不是jar文件),则可以自定义其类,如下所示:

IMO, if your project uses Google's volley as a module (not jar file), you can customize its classes like the following:

选项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 <T> Request<T> add(Request<T> request) {内进行以下检查:

then, inside public <T> Request<T> add(Request<T> request) {, you check as the following:

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

第二个文件,Volley.java:

public static RequestQueue newRequestQueue(Context context, HttpStack stack, boolean cacheUsed) {
        File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);

        String userAgent = "volley/0";
        try {
            String packageName = context.getPackageName();
            PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
            userAgent = packageName + "/" + info.versionCode;
        } catch (NameNotFoundException e) {
        }

        if (stack == null) {
            if (Build.VERSION.SDK_INT >= 9) {
                stack = new HurlStack();
            } else {
                // Prior to Gingerbread, HttpUrlConnection was unreliable.
                // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
                stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
            }
        }

        Network network = new BasicNetwork(stack);

        RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network, cacheUsed);
        queue.start();

        return queue;
    }

public static RequestQueue newRequestQueue(Context context, boolean cacheUsed) {
        return newRequestQueue(context, null, cacheUsed);
    }

最后,在MainActivity中,例如:

如果要使用可用缓存:

RequestQueue requestQueue = Volley.newRequestQueue(this, true); 

如果不想使用可用的缓存:

If don't want to use available cache:

RequestQueue requestQueue = Volley.newRequestQueue(this, false); 


选项2:

Request.java:

添加类变量public boolean mSkipAvailableCache = false;

RequestQueue.java:

public <T> Request<T> add(Request<T> request)内部,您需要进行以下检查:

inside public <T> Request<T> add(Request<T> request), you check as the following:

        // 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;

将不使用可用的缓存. 希望这会有所帮助!

available cache will not be used. Hope this helps!

这篇关于告诉Volley不要使用缓存的数据,而是要发起新请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆