前言
找了些文章,發現說的都不是很清楚.設定始終有點問題
這個配置,每個人的需求不一樣,實現情況肯定也不一樣.
說說我的需求:
1.有網的時候所有介面不使用快取
2.指定的介面產生快取檔案,其他介面不會產生快取檔案
3.無網的時候指定的介面使用快取資料.其他介面不使用快取資料
複製程式碼
1.網路攔截器(關鍵)
提示:只能快取Get請求
Interceptor cacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
//拿到請求體
Request request = chain.request();
//讀介面上的@Headers裡的註解配置
String cacheControl = request.cacheControl().toString();
//判斷沒有網路並且新增了@Headers註解,才使用網路快取.
if (!Utils.isOpenInternet()&&!TextUtils.isEmpty(cacheControl)){
//重置請求體;
request = request.newBuilder()
//強制使用快取
.cacheControl(CacheControl.FORCE_CACHE)
.build();
}
//如果沒有新增註解,則不快取
if (TextUtils.isEmpty(cacheControl) || "no-store" .contains(cacheControl)) {
//響應頭設定成無快取
cacheControl = "no-store";
} else if (Utils.isOpenInternet()) {
//如果有網路,則將快取的過期時間,設定為0,獲取最新資料
cacheControl = "public, max-age=" + 0;
}else {
//...如果無網路,則根據@headers註解的設定進行快取.
}
Response response = chain.proceed(request);
HLog.i("httpInterceptor", cacheControl);
return response.newBuilder()
.header("Cache-Control", cacheControl)
.removeHeader("Pragma")
.build();
};
複製程式碼
具體介面中的使用,新增headers:
/**
* 只能快取get請求.
* 這裡我設定了1天的快取時間
* 介面隨便寫的.哈哈,除了 @Headers(...),其它程式碼沒啥參考價值.
*/
@Headers("Cache-Control: public, max-age=" + 24 * 3600)
@GET("url")
Observable<?> queryInfo(@Query("userName") String userName);
複製程式碼
關於Cache-Control頭的引數說明:
public 所有內容都將被快取(客戶端和代理伺服器都可快取)
private 內容只快取到私有快取中(僅客戶端可以快取,代理伺服器不可快取)
no-cache no-cache是會被快取的,只不過每次在向客戶端(瀏覽器)提供響應資料時,快取都要向伺服器評估快取響應的有效性。
no-store 所有內容都不會被快取到快取或 Internet 臨時檔案中
max-age=xxx (xxx is numeric) 快取的內容將在 xxx 秒後失效, 這個選項只在HTTP 1.1可用, 並如果和Last-Modified一起使用時, 優先順序較高
max-stale和max-age一樣,只能設定在請求頭裡面。
同時設定max-stale和max-age,快取失效的時間按最長的算。(這個其實不用糾結)
複製程式碼
還有2個引數:
CacheControl.FORCE_CACHE
強制使用快取,如果沒有快取資料,則丟擲504(only-if-cached)
CacheControl.FORCE_NETWORK
強制使用網路,不使用任何快取.
這兩個設定,不會判斷是否有網.需要自己寫判斷. 設定錯誤會導致,資料不重新整理,或者有網情況下,請求不到資料 這兩個很關鍵..可以根據自己的需求,進行切換.
2.設定OkHttpClient
OkHttpClient client = new OkHttpClient.Builder()
//新增log攔截器,列印log資訊,程式碼後面貼出
.addInterceptor(loggingInterceptor)
//新增上面程式碼的攔截器,設定快取
.addNetworkInterceptor(cacheInterceptor)
//這個也要新增,否則無網的時候,快取設定不會生效
.addInterceptor(cacheInterceptor)
//設定快取目錄,以及最大快取的大小,這裡是設定10M
.cache(new Cache(MyApplication.getContext().getCacheDir(), 10240 * 1024))
.build();
複製程式碼
3.完整的程式碼:
public class RetrofitUtil {
/**
* 伺服器地址
*/
private static final String API_HOST = Constant.URLS.BASEURL;
private RetrofitUtil() {
}
public static Retrofit getRetrofit() {
return Instanace.retrofit;
}
private static Retrofit getInstanace() {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
HLog.i("RxJava", message);
}
});
Interceptor cacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
//有網的時候,讀介面上的@Headers裡的註解配置
String cacheControl = request.cacheControl().toString();
//沒有網路並且新增了註解,才使用快取.
if (!Utils.isOpenInternet()&&!TextUtils.isEmpty(cacheControl)){
//重置請求體;
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
}
//如果沒有新增註解,則不快取
if (TextUtils.isEmpty(cacheControl) || "no-store" .contains(cacheControl)) {
//響應頭設定成無快取
cacheControl = "no-store";
} else if (Utils.isOpenInternet()) {
//如果有網路,則將快取的過期事件,設定為0,獲取最新資料
cacheControl = "public, max-age=" + 0;
}else {
//...如果無網路,則根據@headers註解的設定進行快取.
}
Response response = chain.proceed(request);
HLog.i("httpInterceptor", cacheControl);
return response.newBuilder()
.header("Cache-Control", cacheControl)
.removeHeader("Pragma")
.build();
}
};
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(loggingInterceptor)
.addNetworkInterceptor(cacheInterceptor)
.addInterceptor(cacheInterceptor)
.cache(new Cache(MyApplication.getContext().getCacheDir(), 10240 * 1024))
.build();
return new Retrofit.Builder()
.client(client)
.baseUrl(API_HOST)
.addConverterFactory(FastjsonConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
}
private static class Instanace {
private static final Retrofit retrofit = getInstanace();
}
}
複製程式碼
附上HttpLoggingInterceptor
/**
* Created by Sunflower on 2016/1/12.
*/
public class HttpLoggingInterceptor implements Interceptor {
private static final Charset UTF8 = Charset.forName("UTF-8");
public enum Level {
/**
* No logs.
*/
NONE,
/**
* Logs request and response lines.
* <p/>
* Example:
* <pre>{@code
* --> POST /greeting HTTP/1.1 (3-byte body)
* <p/>
* <-- HTTP/1.1 200 OK (22ms, 6-byte body)
* }</pre>
*/
BASIC,
/**
* Logs request and response lines and their respective headers.
* <p/>
* Example:
* <pre>{@code
* --> POST /greeting HTTP/1.1
* Host: example.com
* Content-Type: plain/text
* Content-Length: 3
* --> END POST
* <p/>
* <-- HTTP/1.1 200 OK (22ms)
* Content-Type: plain/text
* Content-Length: 6
* <-- END HTTP
* }</pre>
*/
HEADERS,
/**
* Logs request and response lines and their respective headers and bodies (if present).
* <p/>
* Example:
* <pre>{@code
* --> POST /greeting HTTP/1.1
* Host: example.com
* Content-Type: plain/text
* Content-Length: 3
* <p/>
* Hi?
* --> END GET
* <p/>
* <-- HTTP/1.1 200 OK (22ms)
* Content-Type: plain/text
* Content-Length: 6
* <p/>
* Hello!
* <-- END HTTP
* }</pre>
*/
BODY
}
public interface Logger {
void log(String message);
/**
* A {@link Logger} defaults output appropriate for the current platform.
*/
Logger DEFAULT = new Logger() {
@Override
public void log(String message) {
Platform.get().log(Platform.WARN,message,null);
}
};
}
public HttpLoggingInterceptor() {
this(Logger.DEFAULT);
}
public HttpLoggingInterceptor(Logger logger) {
this.logger = logger;
}
private final Logger logger;
private volatile Level level = Level.BODY;
/**
* Change the level at which this interceptor logs.
*/
public HttpLoggingInterceptor setLevel(Level level) {
if (level == null) throw new NullPointerException("level == null. Use Level.NONE instead.");
this.level = level;
return this;
}
@Override
public Response intercept(Chain chain) throws IOException {
Level level = this.level;
Request request = chain.request();
if (level == Level.NONE) {
return chain.proceed(request);
}
boolean logBody = level == Level.BODY;
boolean logHeaders = logBody || level == Level.HEADERS;
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;
String requestStartMessage = request.method() + ' ' + request.url();
if (!logHeaders && hasRequestBody) {
requestStartMessage += " (" + requestBody.contentLength() + "-byte body)";
}
logger.log(requestStartMessage);
if (logHeaders) {
if (!logBody || !hasRequestBody) {
logger.log("--> END " + request.method());
} else if (bodyEncoded(request.headers())) {
logger.log("--> END " + request.method() + " (encoded body omitted)");
} else if (request.body() instanceof MultipartBody) {
//如果是MultipartBody,會log出一大推亂碼的東東
} else {
Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
Charset charset = UTF8;
MediaType contentType = requestBody.contentType();
if (contentType != null) {
contentType.charset(UTF8);
}
logger.log(buffer.readString(charset));
// logger.log(request.method() + " (" + requestBody.contentLength() + "-byte body)");
}
}
long startNs = System.nanoTime();
Response response = chain.proceed(request);
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
logger.log(response.code() + ' ' + response.message() + " (" + tookMs + "ms" + ')');
return response;
}
private boolean bodyEncoded(Headers headers) {
String contentEncoding = headers.get("Content-Encoding");
return contentEncoding != null && !contentEncoding.equalsIgnoreCase("identity");
}
private static String protocol(Protocol protocol) {
return protocol == Protocol.HTTP_1_0 ? "HTTP/1.0" : "HTTP/1.1";
}
}
複製程式碼
您的喜歡與回覆是我最大的動力-_-