【问题标题】:Volley Offline Working排球离线工作
【发布时间】:2015-12-27 23:12:21
【问题描述】:

实现 volley json 响应缓存的方法。我尝试了以下方式从 volley 获取响应。我得到正确的响应。我不知道如何将这些 json 值存储到 volley 缓存中

StringRequest strReq = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    System.out.println("mainresp$$$"+response);
                    Log.d("Volley Request Success", response.toString());
                    result=response;
                    callback.onSuccess(result);


                }
            }, new Response.ErrorListener() {

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

        }
    }) {

        @Override
        protected Map<String, String> getParams() {


            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

【问题讨论】:

  • 我不知道如何实现此功能..上述链接中提供的...请提供一些想法以继续使用 volley 进行响应缓存
  • 感谢您的回答..我会试试这个
  • 但是为什么要缓存 POST 请求呢? GET 的缓存是合理的 :-)

标签: android caching android-volley


【解决方案1】:

和我的 cmets 一起,您已经阅读了我对以下问题的回答:

Android Setup Volley to use from Cache

我刚刚测试了POST请求,如下代码:

        CacheRequest cacheRequest = new CacheRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
            @Override
            public void onResponse(NetworkResponse response) {
                try {
                    final String jsonString = new String(response.data,
                            HttpHeaderParser.parseCharset(response.headers));
                    // Check if it is JSONObject or JSONArray
                    Object json = new JSONTokener(jsonString).nextValue();
                    JSONObject jsonObject = new JSONObject();
                    if (json instanceof JSONObject) {
                        jsonObject = (JSONObject) json;
                    } else if (json instanceof JSONArray) {
                        jsonObject.put("success", json);
                    } else {
                        String message = "{\"error\":\"Unknown Error\",\"code\":\"failed\"}";
                        jsonObject = new JSONObject(message);
                    }
                    textView.setText(jsonObject.toString(5));                        
                } catch (UnsupportedEncodingException | JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                // do something...
            }
        });

我的示例Asp.Net Web API 代码如下:

        // POST api/<controller>
        public IHttpActionResult Post()
        {
            string jsonString = "[" +
                                     "{" +
                                         "name: \"Person 1\"," +
                                         "age: 30," +
                                         "type: \"POST\"," +
                                     "}," +
                                     "{" +
                                         "name: \"Person 2\"," +
                                         "age: 20," +
                                         "type: \"POST\"," +
                                     "}," +
                                     "{" +
                                         "name: \"Person 3\"," +
                                         "age: 40," +
                                         "type: \"POST\"," +
                                     "}" +
                                "]";            
            JArray jsonObj = JArray.Parse(jsonString);
            return Ok(jsonObj);
        }

这是结果截图

【讨论】:

  • 很高兴它能帮到你!
  • 1) 网络不可用... 1) 检查缓存中相应 url 的响应 ...2) 从缓存中检索响应
  • 1) 连接到互联网时.....1) api 调用重新开始.....2) 将响应存储在缓存中
  • IMO,你可以阅读更多here
  • final long cacheHitButRefreshed = 3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background final long cacheExpired = 24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
猜你喜欢
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多