使用Retrofit Android进行缓存

时间:2016-08-15 16:23:49

标签: android caching retrofit2

改造用户, 请用Retrofit进行缓存问题。 我有一个发送以下响应的服务器

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
与Volley一起,响应得到了正确的缓存,但是Retrofit从未做过。我已经像这样设置了改装客户端

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

和网络拦截器

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

已创建缓存文件夹,但其中唯一的文件夹是“journal”文件夹。请问我需要在服务器或Android客户端中更改或添加什么?我会感激任何帮助

1 个答案:

答案 0 :(得分:0)

您忘了将Cache-Control添加到标题中,例如

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