Skip to content Skip to sidebar Skip to footer

Prevent Volley From Sending Cached Parameters On Post Request Method When Caching Is Implemented In Volley

I want volley to cache the response obtained from the server. Hence, I have implemented the caching code (ParseNetworkResponse). But volley is sending cached parameters when a POST

Solution 1:

in Volley's default Network implementation BasicNetwork you have the folloin line on performRequest(96):

addCacheHeaders(headers, request.getCacheEntry());

this is the cause why you have this behavior. So obviously if you want to have a cache entry and at the same time do not send one then you need to make your own implementation/copy 'CustomCacheNetwork' and replace this line for your POST requests.

then you need to create your queue like this:

    Network network = newCustomCacheNetwork(stack);
    RequestQueue queue = newRequestQueue(newDiskBasedCache(cacheDir), network);

This behavior however is strange because if you have valid cache you will not go to the network dispatcher at all.

Solution 2:

This is not a solution to the problem but a workaround:

currentURL = url;
if (methodType.equals("POST")) {
   currentURL = currentURL + "?timestamp=" + String.valueOf(System.currentTimeMillis());
}

Here a dummy parameter which is timestamp is being sent to the website. The website does not use this parameter and hence, it changes nothing. It just provides a unique url to volley every time a post request is executed. After the period of cache storage, i.e. 24hrs in my case, the cached data for the url with timestamp is deleted hence, cache doesn't buildup.

Note: this workaround is not recommended for url where large amount of data is sent or received and large volume of post request to the url are made as the cache may get full.

Post a Comment for "Prevent Volley From Sending Cached Parameters On Post Request Method When Caching Is Implemented In Volley"