gpt4 book ai didi

android-volley - 具有不同 POST 请求的 Android Volley 缓存

转载 作者:行者123 更新时间:2023-12-04 10:04:01 30 4
gpt4 key购买 nike

我正在使用 Android Volley 缓存请求,这在我使用 GET 时工作正常,但由于某些原因我改用 POST。现在我想用不同的 POST 数据缓存相同的 URL。

  • 请求 1 -> URL1,POST 数据 = "Cat=1"
  • 请求 2 -> URL1,POST 数据 = "Cat=2"
  • 请求 3 -> URL1,POST 数据 = "Cat=3"

  • 这可以用 Android Volley 完成吗

    最佳答案

    Volley.Request.getCacheKey()返回在我的情况下相同的 URL;这对我不起作用。

    相反,我不得不在我的子类中覆盖 getCacheKey() 以返回 URL+POST(key=Value)

    这样我就能够缓存对同一 URL 使用不同 POST 数据发出的所有 POST 请求。

    当您尝试检索缓存的请求时,您需要以相同的方式构造缓存键。

    所以这是我的代码的快照:

    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;
    }
    }

    当你构造一个新的请求时,你可以使用 getCacheKey() 在将它放入请求队列之前首先搜索缓存的请求。

    我希望这有帮助。

    关于android-volley - 具有不同 POST 请求的 Android Volley 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26326105/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com