Android Volley Caching with different POST requests

android-volley

Solution

as the `Volley.Request.getCacheKey()` returns the URL which in my case is the same; this did not work for me.

Instead I had to override getCacheKey() in my child class to return URL+POST(key=Value)

That way I was able to cache all the POST requests made to the same URL with different POST data.

when you try to retrieve the cached request you need to construct the cache key with the same way.

so here is a snapshot of my code:

public class CustomPostRequest extends Request<String> {
    .
    .
    private Map<String, String> mParams;
    .
    .
    public void SetPostParam(String strParam, String strValue)
    {
        mParams.put(strParam, strValue);
    }

    @Override
    public Map<String,String> getParams() {
        return mParams;
    }

    @Override
    public String getCacheKey() {
        String temp = super.getCacheKey();
        for (Map.Entry<String, String> entry : mParams.entrySet())
            temp += entry.getKey() + "=" + entry.getValue();// not do another request
        return temp;
    }
}

When ever you construct a new request you can use getCacheKey() to search for the cached request first before putting it in the requests queue.

I hope this helps.

Problem

I am using Android Volley to cache requests this works fine when I was using GET but I switched to use POST for some reasons. Now I want to cache the same URL with different POST data. - Request 1 -> URL1, POST Data = "Cat=1" - Request 2 -> URL1, POST Data = "Cat=2" - Request 3 -> URL1, POST Data = "Cat=3" is this can be done with Android Volley

Original source