首先需要注意的是:使用OKHttp特有的方法只能對GET請求進行緩存赔桌。(我自己就在這個坑爬了很久,因為我們公司的所有接口全部是POST請求)望众。如果想要對POST請求進行緩存的話還需要進一步的處理跌前。這一點從OKHttp的源碼中也可以找到答案灵份。(Cache類中的put方法,當請求方法不是GET的時候直接返回一個null)
private CacheRequest put(Response response) throws IOException {
String requestMethod = response.request().method();
if (HttpMethod.invalidatesCache(response.request().method())) {
try {
remove(response.request());
} catch (IOException ignored) {
// The cache cannot be written.
}
return null;
}
if (!requestMethod.equals("GET")) {
// Don't cache non-GET responses. We're technically allowed to cache
// HEAD requests and some POST requests, but the complexity of doing
// so is high and the benefit is low.
return null;
}
if (OkHeaders.hasVaryAll(response)) {
return null;
}
Entry entry = new Entry(response);
DiskLruCache.Editor editor = null;
try {
editor = cache.edit(urlToKey(response.request()));
if (editor == null) {
return null;
}
entry.writeTo(editor);
return new CacheRequestImpl(editor);
} catch (IOException e) {
abortQuietly(editor);
return null;
}
}
好瓦胎,下面我們就分別來看在GET和POST方法中緩存的實現(xiàn)根蟹。
一嘁扼、GET緩存
1姑裂、首先定義兩個Interceptor,分別作為有網絡時的攔截器和無網絡時的攔截器馋袜。關于攔截器的自定義,網上到處都是舶斧,我這里就寫了兩個最簡單的欣鳖。
public class NoNetInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
boolean connected = HttpNetUtil.isNetConnected(ETApplication.getInstance());
if (!connected) {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
Log.e("etNet", "have not network and to read local cache");
Response response = chain.proceed(request);
return response.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=3600")
.removeHeader("Pragma")
.build();
}
return chain.proceed(request);
}
}
public class NetInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
boolean connected = HttpNetUtil.isNetConnected(ETApplication.getInstance());
if(connected){
//if have network will cache 90s
Log.e("etNet","online cache 90s");
Response response = chain.proceed(request);
int maxTime = 90;
return response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, max-age=" + maxTime)
.build();
}
//if have not network will immediate return
return chain.proceed(request);
}
}
2、在Retrofit中使用
public class RetrofitUtil {
private static Retrofit retrofit;
//over time
private static final long DUFAULT_TIME_OUT = 5;
/**
* @return retrofit instance
*/
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
File file = new File(ETApplication.getInstance().getApplicationContext().getCacheDir(), "rxCache");
//cache size 10M
int cacheSize = 10 * 1024 * 1024;
Cache cache = new Cache(file, cacheSize);
OkHttpClient client = new OkHttpClient.Builder()
.cache(cache)
.addInterceptor(new NoNetInterceptor())
.addNetworkInterceptor(new NetInterceptor())
.connectTimeout(DUFAULT_TIME_OUT, TimeUnit.SECONDS) //連接超時
.writeTimeout(DUFAULT_TIME_OUT, TimeUnit.SECONDS) //寫入超時
.readTimeout(DUFAULT_TIME_OUT, TimeUnit.SECONDS) //讀取超時
.build();
retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(Urls.severURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
/**
* @return interface instance
*
*/
public static APIRetrofit getAPIRetrofitInstance() {
return getRetrofitInstance().create(APIRetrofit.class);
}
}
二茴厉、POST請求緩存
我的做法是用GreenDao來進行緩存观堂。
攔截器參考:http://blog.csdn.net/u010429311/article/details/59116706
GreenDao參考:http://www.reibang.com/p/4986100eff90
public class RewriteCacheControlInterceptor implements Interceptor {
private EtCacheDao mEtCacheDao;
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String url = request.toString();
Log.e("OkHttp","url:"+url);
Buffer buffer = new Buffer();
request.body().writeTo(buffer);
String params = buffer.readString(Charset.forName("UTF-8"));
Log.e("OkHttp","params:"+params);
mEtCacheDao = ETApplication.getInstance().getDaoSession().getEtCacheDao();
Response response;
if (HttpNetUtil.isNetConnected(ETApplication.getInstance())) {
int maxAge = 60 * 60*24;
Response originalResponse = chain.proceed(request);
MediaType type = originalResponse.body().contentType();
byte[] bs = originalResponse.body().bytes();
response = originalResponse.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control", "public, max-age=" + maxAge)
.body(ResponseBody.create(type, bs))
.build();
Log.e("OKHttp","response:"+new String(bs,"UTF-8"));
EtCache etCache = new EtCache(url+params, new String(bs, "UTF-8"));
List<EtCache> list = mEtCacheDao.queryBuilder()
.where(EtCacheDao.Properties.UrlAndParams.eq(url+params))
.list();
if (list==null||list.size()==0) {
mEtCacheDao.insert(etCache);
}
} else {
String b = "";
List<EtCache> list = mEtCacheDao.queryBuilder()
.where(EtCacheDao.Properties.UrlAndParams.eq(url+params))
.list();
if (list != null && list.size() > 0) {
b = list.get(0).getResponse();
}
Log.e("OkHttp", "request:" + url);
Log.e("OkHttp", "request method:" + request.method());
Log.e("OkHttp", "response body:" + b);
int maxStale = 60 * 60 * 24 * 28;
response = new Response.Builder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.body(ResponseBody.create(MediaType.parse("application/json"), b.getBytes()))
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(200)
.build();
}
return response;
}
}