Retrofit--缓存

package com.jiyun.kaishujianggushi.base.retrofit;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
* Created by dell on 2017/10/31.
*/

public class RetrofitUtils {
private static RetrofitUtils retrofitUtils;
private static Retrofit retrofit;
private static RetrofitService service;
//读超时长,单位:毫秒
public static final int READ_TIME_OUT = 7676;
//连接时长,单位:毫秒
public static final int CONNECT_TIME_OUT = 7676;
private RetrofitUtils(){

}
public static synchronized RetrofitUtils getInstance(){
if(retrofitUtils==null){
retrofitUtils=new RetrofitUtils();
}
return retrofitUtils;
}
public static Retrofit getRetrofit(){
if(retrofit==null){
HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.e("HTTPTAG",message);
}
});
File cachefile=new File(App.context.getCacheDir(),"cache");
Cache cache=new Cache(cachefile,1024*1024*100);
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient client=new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.addInterceptor(mRewriteCacheControlInterceptor)
.addNetworkInterceptor(mRewriteCacheControlInterceptor)
.addInterceptor(mInterceptor)
.addNetworkInterceptor(loggingInterceptor)
.cache(cache)
.build();
retrofit=new Retrofit.Builder()
.client(client)
.baseUrl("https://api.kaishustory.com/")
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build();
}
return retrofit;
}
private static Interceptor mInterceptor=new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request=chain.request().newBuilder()
.addHeader("test","test")
.build();
return chain.proceed(request);
}
};
public static RetrofitService getServicec(){
if(service==null){
service=getRetrofit().create(RetrofitService.class);
}
return service;
}
private static final long CACHE_STALE_SEC = 60 * 60 * 24 * 4;
private static Interceptor mRewriteCacheControlInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String cacheControl = request.cacheControl().toString();
if (!NetworkState.isNetworkAvailable(App.context)) {
request = request.newBuilder()
// .cacheControl(TextUtils.isEmpty(cacheControl) ? CacheControl
// .FORCE_NETWORK : CacheControl.FORCE_CACHE)
.cacheControl(CacheControl.FORCE_CACHE)
.build();
}
Response originalResponse = chain.proceed(request);
if (NetworkState.isNetworkAvailable(App.context)) {
return originalResponse.newBuilder()
.header("Cache-Control", cacheControl)
.removeHeader("Pragma")
.build();
} else {
return originalResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=" +CACHE_STALE_SEC)
.removeHeader("Pragma")
.build();
}
}
};
}

-----------------------------------------------------------------

package com.example.administrator.my_kai_shu.network;

import com.example.administrator.my_kai_shu.bean.Bean1_banner_lunbo;

import retrofit2.Call;
import retrofit2.http.Field ;
import retrofit2.http.FormUrlEncoded ;
import retrofit2.http.GET ;
import retrofit2.http.POST ;
import retrofit2.http.Query ;

/**
* Created by Administrator on 2017/10/28.
*/

public interface Retrofit_interface {
//https://api.kaishustory.com/homeservice/adver/list?userid=
@GET ( "homeservice/adver/list" )
Call<Bean1_banner_lunbo> ret_get( @Query ( "userid" )String key);



@FormUrlEncoded
@POST ()
Call<Bean1_banner_lunbo> ret_post ( @Field ( "userid" ) String key);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@删库跑路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值