0

Retrofit users, Please, I have a problem caching with Retrofit. I have a server which sends the following response

Server: Apache
X-Powered-By: PHP/5.5.38
App-Version: {"id":3,"current_version":"0.2","required":"true","build_date":"17-6-2016"}
Cache-Control: public,max-age=300,only-if-cached, max-stale=86400
Upgrade: h2
Connection: Upgrade
Transfer-Encoding: chunked
Content-Type: application/json

with Volley the response gets cached properly but with Retrofit it never does. I have setup the retrofit client like this

> Cache cache = new Cache(cacheDir, cacheSize);
>         httpClient=new OkHttpClient();
>         httpClient.setCache(cache);
>         httpClient.networkInterceptors().clear();
>         httpClient.networkInterceptors().add(new NetworkInterceptor());
>         restService=new Retrofit.Builder()
>                 .baseUrl(HOST_URL)
>                 .addConverterFactory(GsonConverterFactory.create())
>                 .client(httpClient)
>                 .build()
>                 .create(RestService.class);

and the network interceptor

public class NetworkInterceptor implements Interceptor {
    public static final String DEBUG_TAG = NetworkInterceptor.class.getSimpleName();

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request=chain.request();
        User user=UserHandler.getUser();
        if (user!=null&&user.getToken()!=null){
            Log.d(DEBUG_TAG,"Authorization: "+user.getToken());
            request=request.newBuilder().addHeader("Authorization",user.getToken())
                    .method(request.method(),request.body())
                    .build();
        }
        Response response = chain.proceed(request);
        Log.d(DEBUG_TAG, "Cache-Control: " + response.cacheControl());
        return response;
    }
}

The cache folder is created but the only folder there is the "journal" folder. Please what do I need to change or add in the server or android client? I'll appreciate any help

Somesh Kumar
  • 8,088
  • 4
  • 33
  • 49

1 Answers1

0

You forgot to add Cache-Control to your headers e.g.

NetworkInterceptor implements Interceptor { 

    public static final String DEBUG_TAG = NetworkInterceptor.class.getSimpleName();

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request=chain.request();
        User user=UserHandler.getUser();
        if (user!=null&&user.getToken()!=null){
            Log.d(DEBUG_TAG,"Authorization: "+user.getToken());
            request=request.newBuilder().addHeader("Authorization",user.getToken())
                                        // Only if the response is stored
                                        .addHeader("Cache-Control", "only-if-cached")
                                        // Return cached responses not older than 2419200 seconds (28 days)                                       
                                        //.addHeader("Cache-Control", "max-stale=2419200")
                                        .method(request.method(),request.body())
                                        .build();
        }
        Response response = chain.proceed(request);
        Log.d(DEBUG_TAG, "Cache-Control: " + response.cacheControl());
        return response;
    }
}
JohnWowUs
  • 3,053
  • 1
  • 13
  • 20