缓存 创建 一个类 extends Application 在 manifests 中 配置 name=""
public class Mappliction extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
}
}
日志 拦截器 创建 一个类implements Interceptor
public class Minsterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
//获得请求
Request request = chain.request();
HttpUrl url = request.url();
if (url != null) {
System.out.println("我为拦截器" + url);
}else {
System.out.println("拦截失败");
}
//继续请求
Response proceed = chain.proceed(request);
//返回请求
return proceed;
}
}
创建 OkHttp
File filesDir = Mappliction.context.getFilesDir();
OkHttpClient client = new OkHttpClient.Builder()
//添加拦截器
.addInterceptor(new Minsterceptor())
//缓存
.cache(new Cache(filesDir, 1024 * 1024))
.build();
网友评论