Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 截击缓存未更新_Android_Json_Android Volley - Fatal编程技术网

Android 截击缓存未更新

Android 截击缓存未更新,android,json,android-volley,Android,Json,Android Volley,嗨,我在android应用程序中使用volley jar来缓存离线模式下的json数据。它工作得非常好。这是我的代码 Cache cache1 = AppController.getInstance().getRequestQueue().getCache(); Entry entry1 = cache1.get(URL); if (entry1 != null) { // fetch the data from cache

嗨,我在android应用程序中使用volley jar来缓存离线模式下的json数据。它工作得非常好。这是我的代码

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

        } else {

            AppController.getInstance().getRequestQueue().getCache().remove(URL);
            // making fresh volley request and getting json
            JsonObjectRequest jsonReq1 = new JsonObjectRequest(Method.GET,
                    URL, null, new Response.Listener<JSONObject>() {

                        @Override
                        public void onResponse(JSONObject response) {
                            VolleyLog.d(TAG, "Response: " + response.toString());
                            if (response != null) {
                                parseJsonFeed(response);
                            }
                        }
                    }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            VolleyLog.d(TAG, "Error:" + error.getMessage());
                        }
                    });

            // Adding request to volley request queue
            AppController.getInstance().addToRequestQueue(jsonReq1);
        }





private void parseJsonFeed(JSONObject response) {
        try {
             feedArray = response.getJSONArray("events");

            for (int i = 0; i < feedArray.length(); i++) {

                 feedObj = (JSONObject) feedArray.get(i);


                event_id.add(feedObj.getInt("event_id"));
                event_desc.add(feedObj.getString("event_title"));
                event_date.add(feedObj.getString("event_date"));
                event_place.add(feedObj.getString("event_place"));
                event_time.add(feedObj.getString("event_time"));

            }


             listView.setAdapter(new dataListAdapter(event_date,event_desc,event_place,event_time));





            //Log.i("event1111", event.toString());
        //  Log.i("event2222", event2.toString());
            Toast.makeText(getActivity(), "dataa"+event_id, 5000).show();

        } catch (JSONException e) {
            e.printStackTrace();


  }
     }
Cache cache1=AppController.getInstance().getRequestQueue().getCache();
Entry entry1=cache1.get(URL);
if(entry1!=null){
//从缓存中获取数据
试一试{
data2=新字符串(entry1.data,“UTF-8”);
试一试{
parseJsonFeed(新的JSONObject(data2));
}捕获(JSONException e){
e、 printStackTrace();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
}否则{
AppController.getInstance().getRequestQueue().getCache().remove(URL);
//发出新的截击请求并获取json
JsonObjectRequest jsonReq1=新的JsonObjectRequest(Method.GET,
URL,null,新响应。侦听器(){
@凌驾
公共void onResponse(JSONObject响应){
d(标记“Response:+Response.toString());
if(响应!=null){
parseJsonFeed(响应);
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(标记“Error:+Error.getMessage());
}
});
//将请求添加到截击请求队列
AppController.getInstance().addToRequestQueue(jsonReq1);
}
私有void parseJsonFeed(JSONObject响应){
试一试{
feedArray=response.getJSONArray(“事件”);
对于(int i=0;i

我的问题是我得到了数据。但是当我更新json数据时,缓存数据不会更新。那么我如何根据json数据更改缓存数据。请帮助我提前感谢:)

正确设置服务器的缓存头响应,我认为您需要更改标头的最大期限或任何自定义配置,因为Volley会查看标头,然后将响应存储在缓存中,如下所示:

  headerValue = headers.get("Cache-Control");
        if (headerValue != null) {
            hasCacheControl = true;
            String[] tokens = headerValue.split(",");
            for (int i = 0; i < tokens.length; i++) {
                String token = tokens[i].trim();
                if (token.equals("no-cache") || token.equals("no-store")) {
                    return null;
                } else if (token.startsWith("max-age=")) {
                    try {
                        maxAge = Long.parseLong(token.substring(8));
                    } catch (Exception e) {
                    }
                } else if (token.equals("must-revalidate") || token.equals("proxy-revalidate")) {
                    maxAge = 0;
                }
            }
        }

        headerValue = headers.get("Expires");
        if (headerValue != null) {
            serverExpires = parseDateAsEpoch(headerValue);
        }

        serverEtag = headers.get("ETag");

        // Cache-Control takes precedence over an Expires header, even if both exist and Expires
        // is more restrictive.
        if (hasCacheControl) {
            softExpire = now + maxAge * 1000;
        } else if (serverDate > 0 && serverExpires >= serverDate) {
            // Default semantic for Expire header in HTTP specification is softExpire.
            softExpire = now + (serverExpires - serverDate);
        }

        Cache.Entry entry = new Cache.Entry();
        entry.data = response.data;
        entry.etag = serverEtag;
        entry.softTtl = softExpire;
        entry.ttl = entry.softTtl;
        entry.serverDate = serverDate;
        entry.responseHeaders = headers;
headerValue=headers.get(“缓存控制”);
if(headerValue!=null){
hasCacheControl=true;
字符串[]标记=headerValue.split(“,”);
for(int i=0;i0&&serverExpires>=serverDate){
//HTTP规范中Expire头的默认语义为softExpire。
softExpire=now+(serverExpires-serverDate);
}
Cache.Entry=new Cache.Entry();
entry.data=response.data;
entry.etag=serverEtag;
entry.softTtl=softExpire;
entry.ttl=entry.softTtl;
entry.serverDate=serverDate;
entry.responseHeaders=标题;