【问题标题】:How to get data on OkHttp response code 304?如何获取 OkHttp 响应代码 304 上的数据?
【发布时间】:2014-11-27 09:58:40
【问题描述】:

我读到代码为 304(未修改)的响应应该没有正文。在这种情况下,OkHttp 是从缓存中获取正文,还是我们应该明确地获取它,即

if reponseCode == 304:
      body = <getDataFromCache>

后一种情况,如何从缓存中获取数据?

OkHttpClient client = new OkHttpClient();

File cacheDirectory = new File(context.getCacheDir(), "responses");

Cache cache = null;
try {
    cache = new Cache(cacheDirectory, 10 * 1024 * 1024); // 10M
    client.setCache(cache);
} catch (IOException e) {
    Log.e("AbstractFeedIntentService", "Could not create http cache", e);
}

Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(url);
Request request = requestBuilder.build();

Call call = client.newCall(request);
Response response = call.execute();

// if code==304, does the response contain the data from the cache. If not, how to get it?

【问题讨论】:

    标签: android caching okhttp


    【解决方案1】:

    OkHttp 将从其响应缓存中返回数据。

    【讨论】:

    • 谢谢。这在任何地方都有记录吗?
    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2015-09-11
    • 2020-06-01
    相关资源
    最近更新 更多