通过导入依赖实现okhttp的应用:
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.squareup.okio:okio:1.13.0'
封装get和post两种方式:
package com.example.jddemo10test.utils;
import android.os.Environment;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* 1. 类的用途 封装OkHttp3的工具类 用单例设计模式
* 2. @author forever
* 3. @date 2017/9/6 09:19
*/
public class OkHttp3Utils {
/**
* 懒汉 安全 加同步
* 私有的静态成员变量 只声明不创建
* 私有的构造方法
* 提供返回实例的静态方法
*/
private static OkHttpClient okHttpClient = null;
public OkHttp3Utils() {
}
public static OkHttpClient getInstance() {
if (okHttpClient == null) {
//加同步安全
synchronized (OkHttp3Utils.class) {
if (okHttpClient == null) {
//判空 为空创建实例
// okHttpClient = new OkHttpClient();
/**
* 和OkHttp2.x有区别的是不能通过OkHttpClient直接设置超时时间和缓存了,而是通过OkHttpClient.Builder来设置,
* 通过builder配置好OkHttpClient后用builder.build()来返回OkHttpClient,
* 所以我们通常不会调用new OkHttpClient()来得到OkHttpClient,而是通过builder.build():
*/
// File sdcache = getExternalCacheDir();
File sdcache = new File(Environment.getExternalStorageDirectory(), "cache");
int cacheSize = 10 * 1024 * 1024;
okHttpClient = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS)
.writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS).cache(new Cache(sdcache.getAbsoluteFile(), cacheSize)).build();
}
}
}
return okHttpClient;
}
/**
* get请求
* 参数1 url
* 参数2 回调Callback
*/
public static void doGet(String url, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//创建Request
Request request = new Request.Builder().url(url).build();
//得到Call对象
Call call = okHttpClient.newCall(request);
//执行异步请求
call.enqueue(callback);
}
/**
* post请求
* 参数1 url
* 参数2 回调Callback
*/
public static void doPost(String url, Map<String, String> params, Callback callback) {
//创建OkHttpClient请求对象
OkHttpClient okHttpClient = getInstance();
//3.x版本post请求换成FormBody 封装键值对参数
FormBody.Builder builder = new FormBody.Builder();
//遍历集合
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 = okHttpClient.newCall(request);
call.enqueue(callback);
}
/**
* post请求上传文件
* 参数1 url
* 参数2 回调Callback
*/
public static void uploadPic(String url, File file, String fileName) {
//创建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 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 {
//上传成功回调 目前不需要处理
}
});
}
/**
* Post请求发送JSON数据
* 参数一:请求Url
* 参数二:请求的JSON
* 参数三:请求回调
*/
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);
}
/**
* 下载文件 以流的形式把apk写入的指定文件 得到file后进行安装
* 参数一:请求Url
* 参数二:保存文件的路径名
* 参数三:保存文件的文件名
*/
// public static void download(final SelectActivity context, final String url, final String saveDir) {
// Request request = new Request.Builder().url(url).build();
// 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);
}
}
本文的封装:
封装好两个class:
class:OkHttpUtils
public class OkHttpUtils {
private Handler handler=new Handler();
public Handler getHandler(){
return handler;
}
private static OkHttpUtils okHttpUtils=new OkHttpUtils();
private OkHttpUtils(){};
public static OkHttpUtils getInstance(){
return okHttpUtils;
}
private OkHttpClient client;
private void initOkHttpClient(){
if(client==null){
client=new OkHttpClient().newBuilder().build();
}
}
public void doGet(String url, Callback callback){
initOkHttpClient();
Request request=new Request.Builder().url(url).build();
Call call=client.newCall(request);
call.enqueue(callback);
}
}
第二个class是实现将OkHttp在主线程运行
class:OnUiCallback
public abstract class OnUiCallback implements Callback {
private Handler handler=OkHttpUtils.getInstance().getHandler();
public abstract void onFailed(Call call,IOException e);
public abstract void onSuccess(String result)throws IOException;
@Override
public void onFailure(final Call call, final IOException e) {
handler.post(new Runnable() {
@Override
public void run() {
onFailure(call,e);
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String result=response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
try {
onSuccess(result);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
接下来就可以在activity中写解析的过程了
别忘了加载gson 和网络权限
例如定义一个方法initgsonData();
private void initvpData() {
OkHttpUtils.getInstance().doGet("http://v3.wufazhuce.com:8000/api/reading/index/?version=3.5.0&platform=android", new OnUiCallback() {
@Override
public void onFailed(Call call, IOException e) {
}
@Override
public void onSuccess(String result) throws IOException {
Gson gson=new Gson();
//Max 是gson的最大实现类
Max m = gson.fromJson(result, Max.class);
}
});
}
添加拦截器
日志拦截器依赖:
compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
CacheInterceptor 类:
package com.bawei.zy.lanjiqi2.utils;
import android.util.Log;
import java.io.IOException;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created byd
*/
public class CacheInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
//有网状态下的 最大缓存时间 1分钟
int maxAge = 60;
//无网状态下的 最大缓存时间 1天
int maxStale = 60 * 60 * 24;
Request request = chain.request();
if(NetUtils.isNetAvailable(MyApp.context)){
Log.i("======Available", "intercept: ");
request.newBuilder().cacheControl(CacheControl.FORCE_NETWORK).build();
}else{
Log.i("======not Available", "intercept: ");
request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
if(NetUtils.isNetAvailable(MyApp.context)){
response.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
//cache for cache seconds
.header("Cache-Control", "max-age="+maxAge)
.build();
}else{
response.newBuilder()
.removeHeader("Pragma")
.removeHeader("Cache-Control")
//cache for cache seconds
.header("Cache-Control", "max-stale="+maxStale)
.build();
}
return response;
}
}
在OkHttpUtils类里面添加拦截器:
package com.bawei.zy.lanjiqi2.utils;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
/**
* 1. 类的用途 对OkHttp3二次封装 单例模式
* 2. @author
* 3. @date
*/
public class OkHttp3Utils {
private 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(), "wybcache");
int cacheSize = 10 * 1024 * 1024;
//okhttp拦截器
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(String message) {
Log.i("msg", message.toString());
}
});
//拦截器日志分类
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//网络拦截器
CacheInterceptor cacheInterceptor = new CacheInterceptor();
okHttpClient = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.MINUTES)
//应用拦截起
.addInterceptor(httpLoggingInterceptor)
//网络拦截器
.addNetworkInterceptor(cacheInterceptor)
.writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS)
.cache(new Cache(sdcache.getAbsoluteFile(), cacheSize)).build();
//add
}
}
}
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);
}
/**
* post请求方式
*
* @param url 请求地址
* @param params 请求体
* @param callback 接口回调
*/
public static void doPost(String url, Map<String, String> 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);
}
/**
* 上传文件
*
* @param url 接口地址
* @param file 文件
* @param fileName 文件名
*/
public static void loadFile(String url, File file, String fileName) {
//创建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(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
//请求成功
}
});
}
/**
* 上传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);
}
}