简体   繁体   English

Android Volley访问缓存响应数据

[英]Android Volley access caching response data

I'm using Volley library to access my server data.Volley has inbuilt caching function.I tried to use that as follows.this out put "CACHED RESPONSE". 我正在使用Volley库访问服务器数据。Volley具有内置的缓存功能。我尝试按以下方式使用它。此输出为“ CACHED RESPONSE”。 but i don't know how to access the cached data. 但我不知道如何访问缓存的数据。

 void initHttpCall(){

    RequestQueue queue = Volley.newRequestQueue(mcontext);

    UOBRequest<RateData> myReq = new UOBRequest<RateData>(Method.GET,
                                            Constants.GET_RATES,
                                            RateData.class,
                                            mlistner,
                                            createMyReqErrorListener()){


                            @Override
                            public Map<String, String> getHeaders(){

                            HashMap<String, String> headers = new HashMap<String, String>();
                            headers.put("Authorization", getToken());
                            headers.put("Accept","application/json" );

                            return headers;

                            }



    };

    myReq.setShouldCache(true);
    if(queue.getCache().get(Constants.GET_RATES)!=null){

        System.out.println("CACHED RESPONSE");

        }else{

             queue.add(myReq);
        }


}

} }

This is my response listner and want to get RateData object here. 这是我的响应列表器,想在这里获取RateData对象。

new Response.Listener<RateData>() {



    @Override
    public void onResponse(RateData rateData) {

        setupCurrencyPager(rateData);
        setLastUpdatedTime();

    }
});

You misunderstood how Volley's caching system works. 您误解了Volley的缓存系统的工作方式。 The beauty of it is that as a user of Volley, you are unaware of where the response is coming from. 它的优点在于,作为Volley的用户,您不知道响应来自何处。

When you add a new request to the RequestQueue , Volley checks if that request already has a cached response. 当您将新请求添加到RequestQueue ,Volley将检查该请求是否已经具有缓存的响应。 If it does, and that response has not expired yet, it is returned immediately. 如果是这样,并且该响应尚未过期,则会立即返回。 Otherwise, it goes outside to the network, retrieves the response, caches it and returns it to you. 否则,它将进入网络之外,检索响应,将其缓存并返回给您。

You don't need that last if statement, simply add it to the queue and Volley will take care of the rest. 您不需要最后一个if语句,只需将其添加到队列中,Volley就会处理其余的事情。

try the following code.it will help you sure. 尝试以下代码。它将帮助您确定。

please create a request which you want to pass to server. 请创建您要传递到服务器的请求。 JSONObject request = new JSONObject(); JSONObject request = new JSONObject(); request.put("user","user2"); request.put(“ user”,“ user2”);

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, request, new Response.Listener() { JsonObjectRequest jsObjRequest =新的JsonObjectRequest(Request.Method.POST,URL,请求,新的Response.Listener(){

    @Override
    public void onResponse(JSONObject response) {
        // TODO Auto-generated method stub
        Log.v("response:-"+response);
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
    }
});
queue.add(jsObjRequest);

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM