2017-07-07 179 views
0

這裏有很多關於Retrofit緩存的複雜答案,但沒有一個對我有幫助。Retrofit響應緩存

如何緩存來自Retrofit響應的代碼響應?

我的應用程序工作正常下載下面的JSON,但我需要爲它做一個緩存,我從來沒有在Android上做過緩存。

我需要離線打開應用程序,並從我的Retrofit調用中獲得結果,就像我在線一樣。

public class APIClient { 

public static final String BASE_URL = "https://raw.githubusercontent.com/"; 
private static Retrofit retrofit = null; 

public static Retrofit getClient() { 
    if (retrofit==null) { 
     OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
     httpClient.addInterceptor(new Interceptor() { 
      @Override 
      public Response intercept(Interceptor.Chain chain) throws IOException { 
       Request original = chain.request(); 

       Request request = original.newBuilder() 
         .method(original.method(), original.body()) 
         .build(); 
       return chain.proceed(request); 
      } 
     }); 

     OkHttpClient client = httpClient.build(); 
     retrofit = new Retrofit.Builder() 
       .baseUrl(BASE_URL) 
       .addConverterFactory(GsonConverterFactory.create()) 
       .client(client) 
       .build(); 
    } 
    return retrofit; 
}} 

JSON響應非常簡單:

{ 
 
    "fruits":[ 
 
     { 
 
     "name":"Apple", 
 
     "image":"https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/265px-Red_Apple.jpg", 
 
     "price":35 
 
     }, 
 
     { 
 
     "name":"Banana", 
 
     "image":"https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Bananas_white_background_DS.jpg/320px-Bananas_white_background_DS.jpg", 
 
     "price":12 
 
     }, 
 
     { 
 
     "name":"Grapes", 
 
     "image":"https://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Table_grapes_on_white.jpg/320px-Table_grapes_on_white.jpg", 
 
     "price":45 
 
     }, 
 
     { 
 
     "name":"Pineapple", 
 
     "image":"https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Pineapple_and_cross_section.jpg/286px-Pineapple_and_cross_section.jpg", 
 
     "price":200 
 
     }, 
 
     { 
 
     "name":"cherry", 
 
     "image":"http://www.desicomments.com/wp-content/uploads/2017/05/Cherry-Image-600x570.jpg", 
 
     "price":13 
 
     }, 
 
     { 
 
     "name":"clementine", 
 
     "image":"http://www.icecreamnation.org/wp-content/uploads/2013/04/Clementine_orange.jpg", 
 
     "price":12.4 
 
     }, 
 
     { 
 
     "name":"olive", 
 
     "image":"https://www.homenaturalcures.com/wp-content/uploads/olive.jpg", 
 
     "price":9.5 
 
     }, 
 
     { 
 
     "name":"tomato", 
 
     "image":"http://cdn2.stylecraze.com/wp-content/uploads/2013/05/tomato-hair-benefits1.jpg", 
 
     "price":8.75 
 
     }, 
 
     { 
 
     "name":"huckleberry", 
 
     "image":"http://farm3.static.flickr.com/2131/2082287810_47339fc93e.jpg", 
 
     "price":11.75 
 
     }, 
 
     { 
 
     "name":"papaya", 
 
     "image":"http://media.mercola.com/assets/images/foodfacts/papaya-nutrition-facts.jpg", 
 
     "price":2.75 
 
     }, 
 
     { 
 
     "name":"lime", 
 
     "image":"https://www.florihana.com/images/stories/virtuemart/product/FLE019%20-%20LIME.jpg", 
 
     "price":5.75 
 
     }, 
 
     { 
 
     "name":"pear", 
 
     "image":"https://www.organicfacts.net/wp-content/uploads/pear.jpg", 
 
     "price":4.75 
 
     } 
 
    ] 
 
}

回答

1

假設服務器方面Cache-Control/If-Modified-Since頭,比所有你所要做的就是創建一個Cache對象並設定它到OkHttpClient。改造將照顧其餘的。

 

    int cacheSize = 10 * 1024 * 1024; // 10 MB 
    Cache cache = new Cache(getCacheDir(), cacheSize); 

    OkHttpClient okHttpClient = new OkHttpClient.Builder() 
      .cache(cache) 
      .build(); 

    Retrofit.Builder builder = new Retrofit.Builder() 
      .baseUrl("http://10.0.2.2:3000/") 
      .client(okHttpClient) 
      .addConverterFactory(GsonConverterFactory.create()); 

    Retrofit retrofit = builder.build(); 
 

來源:使用改裝2.0和okHttp futurestud.io

+0

當我用這個例子從futurestud.io它變得無法解析法對「getCacheDir()」。我應該在這裏改變什麼? – BlitzkriegBlue

+0

'getCacheDir()'是'Context'類的一種方法,所以你應該使用'Context'對象或'Activity'(它是'Context'的後代)來做到這一點。 – azizbekian

+0

是的,但是在我的班級APIClient上面它並不「適合」。所以我應該在使Call <>的類上創建緩存。在這裏說「新手」。 – BlitzkriegBlue

1

中序緩存我們的服務器響應,大家都需要的是OkHttpClient,AddNetworkInterceptor添加緩存攔截器,則不能使用。 AddInterceptor,同時也不一樣。

這種方法不需要服務器端操作,也沒有其他服務器端的緩存策略,如果服務器有自己的緩存策略代碼,應該相應修改以適應服務器。

You can refer this link , it helped me to solve my problem

+0

https://stackoverflow.com/help/how-to-answer – Kondal