compile ‘com.liwy.easyhttp:easyhttp:1.0.7’
在Application里初始化,主要初始化内容为统一解析回调和IHttpService实现类传入,具体如下:
// 实例化请求实现类
OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(20, TimeUnit.SECONDS).build();
RequestService requestService = new RequestService(okHttpClient);
// 设置EasyHttp的功能实现类为okHttpService,post提交方式为form表单,数据解析方式为GSON
EasyHttp.getInstance().initHttpService(requestService, Constants.MEDIA_TYPE_FORM,DataParser.PARSE_GSON);
如需自定义数据解析方式,需实现数据解析类的接口,并加入解析回调集合:
// 默认已实现两种解析方式GSON和xml
// 也可以自定义解析方式,比如解析html类型的数据
DataParser.addCallback("html", new Callback() {
@Override
public void onSuccess(String result,SuccessCallback successCallback) {
// do parsing which you want to
}
@Override
public void onError(String error, ErrorCallback errorCallback) {
// http error
}
});
目前共提供四种方法,如下:
1. get(EasyRequest req)
2. post(EasyRequest req)
3. download(EasyRequest req)
4. upload(EasyRequest req)
调用:EasyHttp.getInstance().post(req);
1、 get请求
Map params = new HashMap<>();
params.put("identity", "40283c825d2bca81015d2bcabe850000");
params.put("jsonKey", "test");
EasyRequest easyRequest = EasyHttp.getBuilder()
.setUrl("http://192.168.131.19:8080/cnliwy/appdata/getTestData")
.setParams(params)
.setTag("testTag")
.setSync(true) //设置本次请求为同步,默认为异步请求.
.setSuccessCallback(new SuccessCallback() {
@Override
public void success(String result) {
System.out.println(result);
tvContent.setText(result);
}
})
.build();
EasyHttp.getInstance().get(easyRequest);
参数也可不通过map直接传入,如下
EasyRequest easyRequest = EasyHttp.getBuilder()
.setUrl("http://192.168.131.19:8080/cnliwy/appdata/getTestData")
.addParam("identity", "40283c825d2bca81015d2bcabe850000")
.addParam("jsonKey", "test")
.setTag("testTag")
.setSync(false)
.setSuccessCallback(new SuccessCallback() {
@Override
public void success(String result) {
System.out.println(result);
tvContent.setText(result);
}
})
.build();
如果需要在请求过程中取消该请求,可以如下操作:
EasyHttp.getInstance().cancelHttp("testTag");
2、post请求
EasyRequest easyRequest = EasyHttp.getBuilder()
.setUrl("http://192.168.131.19:8080/cnliwy/appdata/getTestData")
.addParam("identity", "40283c825d2bca81015d2bcabe850000")
.addParam("jsonKey", "getDatas")
.post()
.setSuccessCallback(new SuccessCallback>() {
@Override
public void success(List result) {
for (Data data : result){
System.out.println(data.getName());
}
tvContent.setText("成功获取数据:" + result.toString());
}
})
.setErrorCallback(new ErrorCallback() {
@Override
public void error(String errorMsg) {
System.out.println("请求失败");
}
})
.build();
EasyHttp.getInstance().http(easyRequest);
3、下载
String url = "http://img5q.duitang.com/uploads/item/201506/23/20150623203928_HzBWU.jpeg";
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/img";
String fileName = "moon.jpeg";
EasyRequest easyRequest = EasyHttp.getBuilder()
.setUrl(url)
.setFileName(fileName)
.setTag(tag)
.setSaveDir(filePath)
.setDownloadCallback(new DownloadCallback() {
@Override
public void onSuccess(File o) {
System.out.println("---->下载成功" + o.getAbsolutePath());
}
@Override
public void onError(String err) {
System.out.println("---->下载失败");
}
@Override
public void onProgress(long total, int progress) {
tvContent.setText("已下载%" + progress);
if(progress == 100)tvContent.setText("下载完成");
}
})
.build();
EasyHttp.getInstance().download(easyRequest);
4、上传
// 参数
Map params = new HashMap<>();
params.put("title","upload head icon and apk");
params.put("uploadUser","cnliwy");
params.put("uploadType","image and apk");
List files = getFiles();
EasyHttp.getBuilder().setUrl(url).setParams(params).setFiles(files).setSuccessCallback(new SuccessCallback() {
@Override
public void success(Object result) {
System.out.println("上传成功," + result);
contentTv.setText("上传成功," + result);
}
}).setErrorCallback(new ErrorCallback() {
@Override
public void error(String errorMsg) {
System.out.println(values[0].toString());
contentTv.setText("上传失败"+ values[0].toString());
}
}).postFile();
// 需要上传的文件
public List getFiles(){
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/aliwy/";
List files = new ArrayList<>();
files.add(new EasyFile("image1",filePath + "guide_one.png","image/png",new File(filePath + "guide_one.png")));
files.add(new EasyFile("image2",filePath + "guide_two.png","image/png",new File(filePath + "guide_two.png")));
return files;
}
/**
* 设置本次请求的url
* @param url
* @return
*/
public Builder setUrl(String url);
/**
* 传入context对象
* @param context
* @return
*/
public Builder setContext(Context context);
/**
* 添加http的header集合
* @param headers
* @return
*/
public Builder setHeaders(Map headers);
/**
* 传入自定义的okhttpclient
* @param okHttpClient
* @return
*/
public Builder setOkHttpClient(OkHttpClient okHttpClient);
/**
* 设置用于取消本次请求的tag
* @param tag
* @return
*/
public Builder setTag(Object tag);
/**
* 添加请求参数集合
* @param params
* @return
*/
public Builder setParams(Map params);
/**
* 添加请求参数和值
* @param key
* @param value
* @return
*/
public Builder addParam(String key,Object value);
/**
* 添加http的header信息
* @param key
* @param value
* @return
*/
public Builder addHeader(String key,String value);
/**
* 传入要上传的文件
* @param uploadFiles
* @return
*/
public Builder setUploadFiles(List uploadFiles)
/**
* 自定义requestbody数据,若用此方法,则setMediaTpe和setParam\setParams则不需设置
* @param requestBody
* @return
*/
public Builder requestBody(RequestBody requestBody);
/**
* 设置post提交数据的mediaType类型,默认为form。需与服务器保持一致。
* @param mediaType form或json
* @return
*/
public Builder setMediaType(String mediaType);
/**
* 设置本次请求为同步或异步。默认为异步。
* @param sync
* @return
*/
public Builder setSync(boolean sync) ;
/**
* 设置本次的数据解析方式,xml解析或者gson解析,默认gson解析
* @param parseType xml或者gson,也可是自定义的解析类型。
* @return
*/
public Builder setParseType(String parseType);
/**
* 设置文件下载的存储路径
* @param saveDir
* @return
*/
public Builder setSaveDir(String saveDir) ;
/**
* 设置文件的存储清楚,不传则默认采用下载链接里的文件名称。
* @param fileName
* @return
*/
public Builder setFileName(String fileName);
/**
* 请求成功的结果回调
* @param successCallback
* @return
*/
public Builder setSuccessCallback(SuccessCallback successCallback);
/**
* 请求失败的结果回调
* @param errorCallback
* @return
*/
public Builder setErrorCallback(ErrorCallback errorCallback);
/**
* 下载的结果回调
* @param downloadCallback
* @return
*/
public Builder setDownloadCallback(DownloadCallback downloadCallback);
源码传送门