使用retrofit2和okhttp3缓存请求

时间:2017-08-02 09:32:11

标签: android retrofit2 okhttp3

我正在使用retrofit2来使用缓存拦截器来兑现响应

 @Override
public Response intercept(Chain chain) throws IOException {
    Response originalResponse = chain.proceed(chain.request());
    if (NetworkUtil.isConnected(context)) {
        return originalResponse.newBuilder()
                .header("Cache-Control", "public, max-age=" + MAX_AGE)
                .build();
    } else {
        return originalResponse.newBuilder()
                .header("Cache-Control", "public, only-if-cached, max-stale=" + MAX_STALE)
                .build();
    }
}

但我需要缓存特定的请求而不是全部,请问如何做?

1 个答案:

答案 0 :(得分:1)

commits ... that are not in the history that leads to the <since> to be output.

这里的链包含正在进行的请求。

public Response intercept(Chain chain)

您可以检查此请求并对信息采取行动,例如使用public Response intercept(Chain chain) { Request request = chain.request() } request.url()

详细了解拦截here