Android Volley,使缓存无效并每(x)分钟发出一次新请求 [英] Android Volley, invalidate cache and make fresh request every (x) minutes

查看:81
本文介绍了Android Volley,使缓存无效并每(x)分钟发出一次新请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到最新的答案.我正在用Volley将请求发送到Web API.它返回JSON.我正在使用如下所示的缓存功能,但是我想确保列表视图每隔一段时间刷新一次(比如说现在是30分钟).如何使此给定URL的缓存无效,以使我的应用自动处理(没有刷新按钮).这个问题有助于指出无效和删除之间的区别.

I couldn't find an updated answer for this. I'm sending requests with Volley to a web API. It returns JSON. I'm using the cache feature like below, but I would like to make sure that the listview is refreshed every so often (say 30 mins for now). How can I invalidate the cache for this given URL to have my app handle that automatically (without a refresh button). This question was helpful in pointing out the difference between invalidate and remove.

MainActivity.java

MainActivity.java

Cache cache = AppController.getInstance().getRequestQueue().getCache();
        Entry entry = cache.get(URL_FEED);
        if (entry != null) {
            // fetch the data from cache
            try {
                String data = new String(entry.data, "UTF-8");
                try {
                    parseJsonFeed(new JSONArray(data));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        } else 
        {
            // making fresh volley request and getting json

            JsonArrayRequest getRequest = new JsonArrayRequest(URL_FEED,
                    new Response.Listener<JSONArray>()
                    {
                        @Override public void onResponse(JSONArray response) {
                            VolleyLog.d(TAG, "Response: " + response.toString());
                            if (response != null) {
                                parseJsonFeed(response);
                            }
                            Log.d("Response", response.toString());
                        }
                    },......ErrorListener

推荐答案

要刷新列表视图,您可以使用凌空的 serverDate 获取最初收到响应的日期

To refresh listview,you can use volley's serverDate to get the date for when the response was originally received as

AppController.getInstance().getRequestQueue().getCache().get(url).serverDate

此返回日期时间很长. 并在您的代码中使用minutesdifference函数作为

this return datetime in long. And in your code use minutedifference function as

  public static long getMinutesDifference(long timeStart,long timeStop){
            long diff = timeStop - timeStart;
            long diffMinutes = diff / (60 * 1000);

            return  diffMinutes;
        }

并在您的代码中将此函数称为

and Call this function in your code as

Calendar calendar = Calendar.getInstance();
long serverDate = AppController.getInstance().getRequestQueue().getCache().get(url).serverDate;
if(getMinutesDifference(serverDate, calendar.getTimeInMillis()) >=30){
   AppController.getInstance().getRequestQueue().getCache().invalidate(URL_FEED, true);
}

如果先前的网址响应> = 30分钟,它将使缓存无效.

It will invalidate the cache,if previous url response >=30 minutes.

此( invalidate )可以继续使用此数据,直到进行新的调用并将缓存的响应替换为新的响应为止.

This (invalidate) allows to keep using this data until a new call is made and the cached response is overridden with the new response.

这篇关于Android Volley,使缓存无效并每(x)分钟发出一次新请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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