2016-08-15 84 views
0

改造用戶, 請問,我有一個問題與改進緩存。 我有發送如下響應的服務器緩存與改造Android

服務器:Apache X供電-通過:PHP/38年5月5日 的App-版本:{ 「ID」:3 「CURRENT_VERSION」: 「0.2」, 「required」:「true」,「build_date」:「17-6-2016」} Cache-Control:public,max-age = 300,僅緩存,max-stale = 86400 升級:h2 連接:升級 傳輸編碼:分塊 內容類型:應用程序/ JSON

與排球的響應才能正確緩存,但與改造它從來不會。我已經安裝改型客戶端這樣

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

和網絡攔截

公共類NetworkInterceptor實現攔截器{ 公共靜態最終字符串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; 
} 

}

的緩存文件夾,但唯一的文件夾中有「日記」文件夾中。請問我需要更改或添加服務器或Android客戶端?我會很感激任何幫助

回答

0

你忘了添加緩存控制到你的頭,例如

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