如果的OkHttp进行封装的话会提高代码的复用性,及避免多次创建对象,也方面了我们使用。下面是对Ok3的二次封装
首先添加依赖
前两个为OK需添加依赖,最后一个为拦截器依赖
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okio:okio:1.7.0'
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
下面是封装代码,内含日志,缓存拦截器,这里并没有提供Callback 封装 需要使用者自己添加.
因为代码中的注释的已经非常详细了,在这里我就不过多的介绍了。
public class OkHttp3Utils {
//这里用volatile修饰,是为了避免编译器将对象的初始化指令(创建的过程)进行重新排序,引起的线程安全问题。
private volatile static OkHttpClient okHttpClient = null;
public OkHttp3Utils() {
}
//通过单例模式获取实例
public static OkHttpClient getInstance() {
if (okHttpClient == null) {
//同步代码
synchronized (OkHttp3Utils.class) {
if (okHttpClient == null) {
//缓存路径
File sdcache = new File(Environment.getExternalStorageDirectory(), "lanjieqi");
int cacheSize = 10 * 1024 * 1024;
//okhttp拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("拦截器缓存", message.toString());
}
});
//拦截器日志分类
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
okHttpClient = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.MINUTES)
.addInterceptor(httpLoggingInterceptor)
.addNetworkInterceptor(new CacheInterceptor())
.addInterceptor(new CacheInterceptor())
.writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS)
.cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))
.build();
}
}
}
return okHttpClient;
}
/**
* get请求方式
*
* @param url 请求地址
* @param callback 回调接口
*/
public static void doGet(String url, Callback callback) {
//获取请求对象的实例
OkHttpClient okHttpClient = OkHttp3Utils.getInstance();
//创建Request
Request request = new Request.Builder().url(url).build();
//得到Call
Call call = okHttpClient.newCall(request);
//执行异步请求
call.enqueue(callback);
}
public static void doGetHeader(String url,Map headers, Callback callback) {
//获取请求对象的实例
OkHttpClient okHttpClient = OkHttp3Utils.getInstance();
//创建Request
Request.Builder builder = new Request.Builder();
builder.url(url);
for (String header:headers.keySet())
{
builder.addHeader(header,headers.get(header));
}
Request request=builder.build();
//得到Call
Call call = okHttpClient.newCall(request);
//执行异步请求
call.enqueue(callback);
}
/**
* post请求方式
*
* @param url 请求地址
* @param params 请求体
* @param callback 接口回调
*/
public static void doPost(String url, Map params, Callback callback) {
//获取请求对象的实例
OkHttpClient okHttpClient = OkHttp3Utils.getInstance();
//3.x版本post请求换成FormBody 封装键值对参数
FormBody.Builder builder = new FormBody.Builder();
//遍历map集合给请求体添加值
for (String key : params.keySet()) {
builder.add(key, params.get(key));
}
//创建Request
Request request = new Request.Builder().url(url).post(builder.build()).build();
//获取call
Call call = okHttpClient.newCall(request);
//异步请求
call.enqueue(callback);
}
public static void doPostHeader(String url,Map headers, Map params, Callback callback) {
//获取请求对象的实例
OkHttpClient okHttpClient = OkHttp3Utils.getInstance();
//3.x版本post请求换成FormBody 封装键值对参数
FormBody.Builder builder = new FormBody.Builder();
//遍历map集合给请求体添加值
for (String key : params.keySet()) {
builder.add(key, params.get(key));
}
//创建Request
//创建Request
Request.Builder builder1 = new Request.Builder();
builder1.url(url);
for (String header:headers.keySet())
{
builder1.addHeader(header,headers.get(header));
}
Request request = builder1.post(builder.build()).build();
//获取call
Call call = okHttpClient.newCall(request);
//异步请求
call.enqueue(callback);
}
/**
* 上传文件
*
* @param url 接口地址
* @param file 文件
* @param fileName 文件名
*/
public static void loadFile(String url, File file, String fileName,Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//创建RequestBody 封装file参数
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
//创建RequestBody 设置类型等
RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", fileName, fileBody).build();
//创建Request
Request request = new Request.Builder().url(url).post(requestBody).build();
Call call = okHttpClient.newCall(request);
call.enqueue(callback);
}
/**
* post请求上传文件
* 参数1 url
* 参数2 回调Callback
*/
public static void uploadPic(String url, Map params,Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//创建MultipartBody.Builder
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (Map.Entry entry : params.entrySet()) {
Object value = entry.getValue();
if (value instanceof File) {
File file = (File) value;
//创建RequestBody 封装file参数
builder.addFormDataPart(entry.getKey(), file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file));
} else {
builder.addFormDataPart(entry.getKey(), value.toString());
}
}
//创建RequestBody 设置类型等
RequestBody requestBody = builder.build();
//创建Request
Request request = new Request.Builder().url(url).post(requestBody).build();
//得到Call
Call call = okHttpClient.newCall(request);
//执行请求
call.enqueue(callback);
}
/**
* post请求上传文件
* 参数1 url
* 参数2 回调Callback
*/
public static void uploadPic(final Context context, String url,Map params) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//创建MultipartBody.Builder
MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
for (Map.Entry entry : params.entrySet()) {
Object value = entry.getValue();
if (value instanceof File) {
File file = (File) value;
//创建RequestBody 封装file参数
builder.addFormDataPart(entry.getKey(), file.getName(), RequestBody.create(MediaType.parse("application/octet-stream"), file));
} else {
builder.addFormDataPart(entry.getKey(), value.toString());
}
}
//创建RequestBody 设置类型等
RequestBody requestBody = builder.build();
//创建Request
Request request = new Request.Builder().url(url).post(requestBody).build();
//得到Call
Call call = okHttpClient.newCall(request);
//执行请求
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//上传成功回调 目前不需要处理
Looper.prepare();
Toast.makeText(context, "上传成功", Toast.LENGTH_SHORT).show();
Looper.loop();
}
});
}
/**
* 上传json字符
*
* @param url 接口地址
* @param jsonParams Json串
* @param callback 接口回调
*/
public static void doPostJson(String url, String jsonParams, Callback callback) {
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
Request request = new Request.Builder().url(url).post(requestBody).build();
Call call = getInstance().newCall(request);
call.enqueue(callback);
}
/**
* 下载
*
* @param context
* @param url 下载地址
* @param saveDir 保存的位置
*/
public static void downFile(final Activity context, final String url, final String saveDir) {
//创建Request
Request request = new Request.Builder().url(url).build();
//创建Call
Call call = getInstance().newCall(request);
//同步
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.i("xxx", e.toString());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();
//apk保存路径
final String fileDir = isExistDir(saveDir);
//文件
File file = new File(fileDir, getNameFromUrl(url));
context.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, "下载成功:" + fileDir + "," + getNameFromUrl(url), Toast.LENGTH_SHORT).show();
}
});
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
//apk下载完成后 调用系统的安装方法
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
context.startActivity(intent);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) is.close();
if (fos != null) fos.close();
}
}
});
}
/**
* @param saveDir
* @return
* @throws IOException 判断下载目录是否存在
*/
public static String isExistDir(String saveDir) throws IOException {
// 下载位置
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
if (!downloadFile.mkdirs()) {
downloadFile.createNewFile();
}
String savePath = downloadFile.getAbsolutePath();
Log.e("savePath", savePath);
return savePath;
}
return null;
}
/**
* @param url
* @return 从下载连接中解析出文件名
*/
private static String getNameFromUrl(String url) {
return url.substring(url.lastIndexOf("/") + 1);
}
/**
* 为okhttp添加缓存,这里是考虑到服务器不支持缓存时,从而让okhttp支持缓存
*/
private static class CacheInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
// 有网络时 设置缓存超时时间1个小时
int maxAge = 60 * 60;
// 无网络时,设置超时为1天
int maxStale = 60 * 60 * 24;
Request request = chain.request();
if (NetWorkUtils.isNetWorkAvailable(MyApp.getInstance())) {
//有网络时只从网络获取
request = request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
} else {
//无网络时只从缓存中读取
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
/* Looper.prepare();
Toast.makeText(MyApp.getInstance(), "走拦截器缓存", Toast.LENGTH_SHORT).show();
Looper.loop();*/
Log.e("Tag","缓存拦截器正在加载数据……");
}
Response response = chain.proceed(request);
if (NetWorkUtils.isNetWorkAvailable(MyApp.getInstance())) {
response = response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
response = response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
return response;
}
}
}
这是MyApp的全局配置
千万不要忘记在AndroidManifest中进行配置。
public class MyApp extends Application {
public static MyApp mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static MyApp getInstance() {
return mInstance;
}
}
下面是NetWorkUtils
public class NetWorkUtils {
public static boolean isNetWork(Context context){
//获取联网管理器
ConnectivityManager connectivityManager= (ConnectivityManager)context. getSystemService(Context.CONNECTIVITY_SERVICE);
//网络信息
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null){
return true;
}
return false;
}
}
如果缓存失败提供以下解决思路:
1:查看是否忘记添加读写权限,或者是否被手机权限拒绝了。
2:如果get,post请求是带参数的话,可能参数值不同(例如:随机数、时间戳)导致拼接的url地址不一样,会被认为 是新地址去重新发起请求获取数据,所以会被认为没有缓存数据
以上效果亲测有效!!!