这个问题可能是由于使用了 OkHttp 的缓存机制导致的。解决方式是在 OkHttp 的拦截器中添加一个条件,如果发现缓存已经存在,就直接返回缓存,否则发起请求。
下面是一个示例代码:
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
okhttp3.Response response = chain.proceed(request);
String cacheControl = request.cacheControl().toString();
if (TextUtils.isEmpty(cacheControl)) {
cacheControl = "public, max-age=60";
}
if (response.code() == 200) {
return response.newBuilder()
.header("Cache-Control", cacheControl)
.build();
} else {
return response;
}
}
})
.build();
Request request = new Request.Builder()
.url(url)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, "onFailure: " + e.getMessage());
}
@Override
public void onResponse(Call call, okhttp3.Response response) throws IOException {
String result = response.body().string();
Log.d(TAG, "onResponse: " + result);
}
});