Upgrade returns a cached response

I am using Retrofit in an Android app. When I get into the API with a token to get information about the user, it gives a cached (previous) response. Whenever I log out and log back in, the API gives the previous user detail, I tested the API in Postman, it works fine there.

I tried some solutions that I was looking for but nothing works.

The title of the response I receive

Transfer-coding: chunked Content-Type: application / json; encoding = UTF-8 Server: kestrel Date: Mon, Jan 08 2018 09:35:26 GMT

Below ApiClientclass

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

}
+1
source share
1 answer

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)

, .

+1

All Articles