質問

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". 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.

new Response.Listener<RateData>() {



    @Override
    public void onResponse(RateData rateData) {

        setupCurrencyPager(rateData);
        setLastUpdatedTime();

    }
});
役に立ちましたか?

解決

You misunderstood how Volley's caching system works. The beauty of it is that as a user of Volley, you are unaware of where the response is coming from.

When you add a new request to the RequestQueue, Volley checks if that request already has a cached response. 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.

他のヒント

try the following code.it will help you sure.

please create a request which you want to pass to server. JSONObject request = new JSONObject(); request.put("user","user2");

JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, url, request, new 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帰属
所属していません StackOverflow
scroll top