这是之前写的两个关于Ok的缓存类:
只需要把这两个类粘进去就好了,记得添加依赖和网络权限。
--------------------------------------------------------------分割线------------------------------------------------------
Utils 类:
package com.jiyun.kaishu.Model;
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Callback;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by lenovo on 2017/10/30.
*/
public class Utils {
private static Utils utils;
private OkHttpClient okHttpClient;
private Context context;
private Utils(Context context) {
this.context=context;
getOk();
}
private OkHttpClient getOk() {
if (okHttpClient == null) {
synchronized (OkHttpClient.class) {
if (okHttpClient == null) {
initOk(context);
}
}
}
return okHttpClient;
}
private void initOk(final Context context) {
File httpCacheDirectory = new File("/sdcard", "cache_xx");
// Cache cache = new Cache(this.getCacheDir(), 10240*1024);
Cache cache = new Cache(httpCacheDirectory, 10240*1024*10); //10M
OkHttpClient okHttpClient1 = new OkHttpClient();
okHttpClient = okHttpClient1.newBuilder()
.addNetworkInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Log.d("MainActivity", "================进入拦截器");
Request request = chain.request();
Log.e("MainActivity", "新请求 =request==" + request.toString());
Response response = chain.proceed(request); //===========
Response response_build;
if (NetUtil.checkNet(context)) {
int maxAge = 60 * 60 * 24; // 有网络的时候从缓存1天后失效
response_build = response.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
int maxStale = 60 * 60 * 24 * 28; // // 无网络缓存保存四周
response_build = response.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
return response_build;
}
})
.cache(cache)
.connectTimeout(20, TimeUnit.SECONDS)
.readTimeout(20, TimeUnit.SECONDS)
.build();
}
public static synchronized Utils getInstance(Context context){
if (utils==null){
utils=new Utils(context);
}
return utils;
}
public void sendGet(String url, Callback callback){
Request build = new Request.Builder().url(url).build();
okHttpClient.newCall(build).enqueue(callback);
}
}
-------------------------------------------------华丽的分割线-------------------------------------------------------------
NetUtil 类:
package com.jiyun.kaishu.Model;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* Created by DELL on 2017-11-4.
*/
public class NetUtil {
//判断当前手机联网的渠道
public static boolean checkNet(Context context){
boolean wifiConnected = isWIFIConnected(context);
boolean mobileConnected = isMOBILEConnected(context);
boolean youxian = isYouXian(context);
if(wifiConnected==false && mobileConnected== false&&youxian==false){
//如果都没有连接,提示用户当前没有网络
return false;
}
return true;
}
private static boolean isYouXian(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
if (networkInfo != null && networkInfo.isConnected()) {
return true;
}
return false;
}
public static boolean isMOBILEConnected(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(networkInfo !=null && networkInfo.isConnected()){
return true;
}
return false;
}
private static boolean isWIFIConnected(Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(networkInfo !=null && networkInfo.isConnected()){
return true;
}
return false;
}
// 新方法
public static boolean isConn(Context context){
boolean bisConnFlag=false;
ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = conManager.getActiveNetworkInfo();
if(network!=null){
bisConnFlag=conManager.getActiveNetworkInfo().isAvailable();
}
return bisConnFlag;
}
}