OkHttpClient
.Builder builder =
new
OkHttpClient
.Builder();
builder.connectTimeout(
DEFAULT_TIMEOUT
, TimeUnit.
SECONDS
);
builder.writeTimeout(
DEFAULT_TIMEOUT
, TimeUnit.
SECONDS
);
builder.readTimeout(
DEFAULT_TIMEOUT
, TimeUnit.
SECONDS
);
if
(Ln.
isDebugEnabled
()) {
HttpLoggingInterceptor loggingInterceptor =
new
HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.
BASIC
);
builder.interceptors().add(loggingInterceptor);
}
builder.cache(
new
Cache(
new
File(context.getFilesDir(),
"responses"
),
CONST_10
*
CONST_1024
*
CONST_1024
)); //设置缓存存在路径和大小
if
(DevSettingStore.
isDevChannel
()) {
builder.interceptors().add(
new
DevUrlInterceptor(context));
}
builder.interceptors().add(
new
NetStatisticsInterceptor());
// 无网络时,缓存一天
builder.interceptors().add(new LocalCacheInterceptor(context, NetAccessStrategy.
DEFAULT_MAX_LOCAL_CACHE_SECONDS
));
// 有网络时,缓存时间设置
builder.networkInterceptors().add(0, new NetCacheInterceptor(NetAccessStrategy.
DEFAULT_MAX_NET_CACHE_SECONDS
));
builder.build();
|
package
common.retrofit.interceptor;
import
android.content.Context;
import
android.text.TextUtils;
import
common.retrofit.NetAccessStrategy;
import
common.utils.NetworkUtils;
import
java.io.IOException;
import
java.util.concurrent.TimeUnit;
import
okhttp3.CacheControl;
import
okhttp3.Interceptor;
import
okhttp3.Request;
import
okhttp3.Response;
public class
LocalCacheInterceptor
implements
Interceptor {
private int
maxCacheSeconds
;
private
Context
context
;
public
LocalCacheInterceptor(Context context,
int
maxCacheSeconds) {
this
.
context
= context;
this
.
maxCacheSeconds
= maxCacheSeconds;
}
@Override
public
Response intercept(Chain chain)
throws
IOException {
Request request = chain.request();
String header = request.header(
"Cache-Control"
);
if
(TextUtils.
isEmpty
(header)) {
if
(!NetworkUtils.
isNetworkAvailable
(
context
)) {
Request.Builder builder = request.newBuilder();
//if network not available, load in cache
CacheControl cacheControl = new CacheControl.Builder().maxAge(Integer.
MAX_VALUE
, TimeUnit.
SECONDS
)
.maxStale(maxCacheSeconds, TimeUnit.
SECONDS
).build();
request = builder.cacheControl(cacheControl).build();
return chain.proceed(request);
}
}
else if
(NetAccessStrategy.
NO_CONTROL
.equals(header)) {
Request.Builder builder = request.newBuilder();
if
(NetworkUtils.
isNetworkAvailable
(
context
)) {
builder.removeHeader(
"Cache-Control"
);
}
else
{
//if network not available, load in cache
CacheControl cacheControl = new CacheControl.Builder().maxAge(Integer.
MAX_VALUE
, TimeUnit.
SECONDS
)
.maxStale(maxCacheSeconds, TimeUnit.
SECONDS
).build();
builder.cacheControl(cacheControl);
}
return
chain.proceed(builder.build());
}
else if
(header.startsWith(NetAccessStrategy.
NET_REQUEST
)) {
Request.Builder builder = request.newBuilder();
int
[] timeArray = NetAccessStrategy.
getRequestCacheTime
(header);
CacheControl cacheControl =
new
CacheControl.Builder().maxAge(timeArray[
0
], TimeUnit.
SECONDS
).build();
NetAccessStrategy.
setThreadLocalCacheTime
(timeArray[
1
]);
builder.cacheControl(cacheControl);
return
chain.proceed(builder.build());
}
return
chain.proceed(request);
}
}
|
package
common.retrofit.interceptor;
import
android.text.TextUtils;
import
common.retrofit.NetAccessStrategy;
import
java.io.IOException;
import
okhttp3.Interceptor;
import
okhttp3.Request;
import
okhttp3.Response;
public class
NetCacheInterceptor
implements
Interceptor {
private int
maxCacheSeconds
;
public
NetCacheInterceptor(
int
maxCacheSeconds) {
this
.
maxCacheSeconds
= maxCacheSeconds;
}
@Override
public
Response intercept(Chain chain)
throws
IOException {
Request request = chain.request();
int
time = NetAccessStrategy.
getThreadLocalCacheTime
();
if
(TextUtils.
isEmpty
(request.header(
"Cache-Control"
))) {
Request.Builder builder = request.newBuilder();
request = builder.build();
Response originalResponse = chain.proceed(request);
return
originalResponse.newBuilder().header(
"Cache-Control"
,
"public, max-age="
+
maxCacheSeconds
)
.build();
}
else
{
Request.Builder builder = request.newBuilder();
request = builder.build();
Response originalResponse = chain.proceed(request);
if
(time >
0
) {
return
originalResponse.newBuilder().header(
"Cache-Control"
,
"public, max-age="
+ time)
.build();
}
return
originalResponse.newBuilder().header(
"Cache-Control"
, request.header(
"Cache-Control"
))
.build();
}
}
}
|