【问题标题】:Retrofit is returning cached response改造正在返回缓存的响应
【发布时间】:2018-01-07 18:43:40
【问题描述】:

我在 Android 应用程序中使用 Retrofit。当我使用令牌访问 API 以获取用户信息时,它会提供缓存(先前)响应。每当我注销并再次登录时,API 都会提供以前的用户详细信息,我在 Postman 中测试了 API,它在那里工作正常。

我尝试了一些我搜索的解决方案,但没有任何效果。

我得到的响应标头是

传输编码:分块 内容类型:应用程序/json;字符集=utf-8 服务器:红隼 日期:2018 年 1 月 8 日星期一 09:35:26 GMT

下面是ApiClient

public class ApiClient {

public static final String BASE_URL = "http://XX.XXX.XXX.XX/api/";
private static Retrofit authRetrofit = null;
private static Retrofit retrofit = null;

public static Retrofit getClient() {

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }
    return retrofit;
}

 public static Retrofit getAuthorizeClient(final String token) {

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

    httpClient.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();

            Request request = original.newBuilder()
                    .addHeader("Authorization", "Bearer " + token)
                    .addHeader("Cache-control", "no-cache")
                    .method(original.method(), original.body())
                    //.cacheControl(CacheControl.FORCE_NETWORK)
                    .build();

            return chain.proceed(request);
        }
    });

    OkHttpClient client = httpClient.cache(null).build();

    if (authRetrofit == null) {
        authRetrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .client(client).build();
    }
    return authRetrofit;
}

}

【问题讨论】:

  • API 的响应是什么,包括 (http) 标头?通常改造会尊重来自 http 标头的缓存信息。
  • 我的响应是它提供了 JSON 对象,该对象具有两个字段用户名和电子邮件,但它是以前登录的用户的详细信息
  • 还有 http 标头?
  • 抱歉回复晚了。我在响应标头Transfer-Encoding: chunked Content-Type: application/json; charset=utf-8 Server: Kestrel Date: Mon, 08 Jan 2018 09:35:26 GMT 中得到了这个
  • 所以API没有发送缓存控制头?

标签: android retrofit2


【解决方案1】:

在您的httpClient 拦截器中,尝试添加缓存控制标头:

 .addHeader("Cache-control", "no-cache");

编辑

刚刚注意到你在Retrofit2

original.newBuilder().header("Cache-control", "no-cache");

或者尝试将其作为注释添加到您的 API 方法中:

@Headers("Cache-control: no-cache")
Response callApi();

根据Docs.

编辑 2

好的,我怀疑这是因为您的if 条件,如果条件失败,您的authRetrofit 将不会更新。尝试删除它

if (authRetrofit == null)

帮帮忙。

【讨论】:

  • 感谢吉耶的回答,你可以查看我的代码我已经试过了,它不起作用,我已经评论了那行。
  • @AshishTiwari 我在你的代码中没有看到那个指令,他说no-cache,你试过no-store
  • 我昨天试过了!等等我再试一次。我以两种方式指定了它 1-.cacheControl(new CacheControl.Builder().noCache().build()) 2-`.addHeader("Cache-control", "no-cache");`
  • 还是同样的问题。
  • 我在注销时将改造客户端设置为空。它现在工作。谢谢
【解决方案2】:

改用 Post 请求,我遇到了同样的问题,通过更改我的 GET 方法来发布和发送一个随机字符串作为字段来纠正。

【讨论】:

    猜你喜欢
    • 2018-05-16
    • 1970-01-01
    • 2017-01-15
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2016-12-06
    相关资源
    最近更新 更多