【问题标题】:Cache for only specific URLs with retrofit仅缓存特定 URL 并进行改造
【发布时间】:2022-01-27 10:27:58
【问题描述】:

我只需要在我的应用中缓存特定 url 地址的响应;但是我通过许多搜索发现的是响应大小限制的缓存; 任何人都知道一种仅针对特定网址进行缓存的方法吗?

【问题讨论】:

    标签: java android kotlin retrofit okhttp


    【解决方案1】:

    您可以为此https://square.github.io/okhttp/4.x/okhttp/okhttp3/-cache/ 使用CacheControl。在创建调用之前自行检查 URL 并自行设置,或将逻辑放入应用程序拦截器中。

    Request request = new Request.Builder()
        .cacheControl(CacheControl.Builder()
          .noCache()
          .build())
        .url("http://publicobject.com/helloworld.txt")
        .build();
    

    教程https://developpaper.com/okhttp-cache-usage-guide/

    【讨论】:

      猜你喜欢
      • 2018-01-18
      • 2023-03-27
      • 2014-11-25
      • 2018-11-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-13
      相关资源
      最近更新 更多