我正在使用改型向Woocommerce发送请求并从服务器获取数据,我需要缓存数据。
我使用了翻新库提供的工具来缓存数据。没用我在Internet上搜索并测试了许多解决方案。我取得了成功的结果,但仍然无法正常工作。
我的代码:
Api:
@GET(URLs.EndPoint_Products)
Call<List<Salons>> getProductsCategorized(
@Query("category") String category,
@Query("per_page") int per_page
);
请求方法:
private void okhttpReq(int categoryId) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OAuthInterceptor oauth1Woocommerce = new OAuthInterceptor.Builder()
.consumerKey(getString(R.string.cunsomer_key))
.consumerSecret(getString(R.string.consumersecret))
.build();
OkHttpClient client = new OkHttpClient
.Builder()
.cache(new Cache(getContext().getCacheDir(), 10 * 1024 * 1024)) // 10 MB
.addInterceptor(oauth1Woocommerce)
.addInterceptor(interceptor)
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (isOnline()) {
request = request.newBuilder().header("Cache-Control", "public, max-age=" + 88888860).build();
} else {
request = request.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + 60 * 60 * 24 * 7).build();
}
// request = request.newBuilder().header("Cache-Control", "public, max-age=" + 60).build();
return chain.proceed(request);
}
})
.build();
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit mRetrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build();
Api api = mRetrofit.create(Api.class);
Call callSalons = api.getProductsCategorized(String.valueOf(categoryId), 100);
callSalons.enqueue(new Callback<List<Salons>>() {
@Override
public void onResponse(Call<List<Salons>> call, Response<List<Salons>> response) {
if (response.raw().cacheResponse() != null) {
Log.e("Network", "response came from cache");
}
if (response.raw().networkResponse() != null) {
Log.e("Network", "response came from server");
}
salonList.clear();
Log.e("response1", response.headers().toString());
if (response.isSuccessful()) {
try {
assert response.body() != null;
for (int i = 0; i < response.body().size(); i++) {
Salons array = response.body().get(i);
//adding the product to product list
try {
salonList.add(new Salon(
array.getId(),
array.getName(),
descriptionCorrector(array.getShort_description()),
array.getAverage_rating(),
Double.valueOf(array.getPrice()),
array.getCategories(),
array.getAttributes(),
array.getImages()
));
} catch (Exception e) {
// Log.d("retrofit error", e.getMessage());
}
}
productAdapter();
} catch (Exception e) {
Log.d("sizeerror", e.getMessage());
}
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (isStart) {
isStart = false;
okhttpReq_category();
}
} else {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
okhttpReq_category();
}
}
@Override
public void onFailure(Call<List<Salons>> call, Throwable t) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});
}
此请求成功,但总是返回服务器的响应,而不是缓存。
我在Stackoverflow上检查了很多答案,但是没有一个对我不起作用。我的案例与其他这些类似问题之间的唯一区别是:
.addInterceptor(oauth1Woocommerce)
用于Woocommerce身份验证。
我检查了几次,但必须联机才能成功请求,并且总是从服务器而不是缓存中检索数据。
能请你帮我吗?
预先感谢