Volley离线工作

时间:2015-09-30 08:33:55

标签: android caching android-volley

如何实现volley json响应缓存。我尝试以下方式从volley.i获取响应正确获取响应。我不知道如何将这些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);

1 个答案:

答案 0 :(得分:3)

与我的评论一起,您已经在以下问题中阅读了我的答案:

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);
        }

以下是结果截图

result screenshot