相关疑难解决方法(0)

Android Volley + JSONObjectRequest缓存

public class CustomRequest extends JsonObjectRequest {

    public CustomRequest(String url, JSONObject params,
            Listener<JSONObject> listener, ErrorListener errorListener)
            throws JSONException {
        super(Method.POST,url, params, listener,
                errorListener);
        this.setShouldCache(Boolean.TRUE);
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望这段代码足以让我得到隐式缓存的响应.我不确定它是否有效,因为我在发送请求时假设:

  1. 它会首先点击缓存并将其发送到响应

  2. 然后,当结果从远程服务器传来时,它会将其提供给响应

更新:

我想到了如何手动检索缓存并将其重建为JSONObject并通过OnResponse函数发送它,但考虑到隐式缓存,这似乎并不高效.JsonObjectRequest类应该返回JSONObject作为缓存条目而不是原始响应数据.

但我仍然有兴趣知道我是否犯了一些错误.

模糊性完全是由于缺乏文档,所以如果我遗漏了一些非常明显的东西,我会道歉.

android caching response request android-volley

49
推荐指数
2
解决办法
4万
查看次数

Android排球请求是否自动缓存?

我已经遵循了一些教程,如果你想使用缓存结果进行HTTP调用,特别是要显示以下内容.

Cache cache = MyApplication.getInstance().getRequestQueue().getCache();
Cache.Entry entry = cache.get(url);
if (entry != null) {
  // means there is a cached result, to use it we have to do something like this...
  new JSONObject(new String(entry.data, "UTF-8"))
} else {
  // no cached result found so make the full request
  JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                url, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        //stuff
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                //stuff
            }
        });

  // …
Run Code Online (Sandbox Code Playgroud)

android caching android-volley

2
推荐指数
1
解决办法
4516
查看次数

标签 统计

android ×2

android-volley ×2

caching ×2

request ×1

response ×1