Retrofitキャッシュについてここにはたくさんの複雑な答えがありますが、どれも私を助けてくれません。改造応答キャッシュ
Retrofitレスポンスのコードレスポンスをキャッシュするにはどうすればよいですか?
My Appは下の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レスポンスは非常に単純です:あなたがしなければならないすべてはCache
オブジェクトを作成し、それを設定するよりも、サーバーの点にCache-Control
/If-Modified-Since
ヘッダを想定すると
{
"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
}
]
}
それは「getCacheDir()」には解決できないメソッドを取得しますfuturestud.ioから、私はこの例を使用する場合。私はここで何を変えるべきですか? – BlitzkriegBlue
'getCacheDir()'は 'Context'クラスのメソッドなので、' Context'オブジェクトか 'Activity'(' Context'の子孫です)を使って行うべきです。 – azizbekian
ええ、上の私のクラスクラスAPIClientでは、それは "適合"しません。だから、コール<>を行うクラスのキャッシュを作成する必要がありますか?ここで "新宗教"を叫ぶ。 – BlitzkriegBlue