最简单的UrlHttpUtils封装,CallBack方法执行在UI线程。支持get请求,post请求,支持文件上传和下载。
代码很简单,只有五个Java文件,建议下载后将Java文件拷贝到工程中使用。
String url = "https://www.baidu.com/";
UrlHttpUtil.get(url, new CallBackUtil.CallBackString() {
@Override
public void onFailure(int code, String errorMessage) {
}
@Override
public void onResponse(String response) {
}
});
String url = "https://www.baidu.com/";
HashMap<String, String> paramsMap = new HashMap<>();
paramsMap.put("title","title");
UrlHttpUtil.post(url, paramsMap, new CallBackUtil.CallBackString() {
@Override
public void onFailure(int code, String errorMessage) {
}
@Override
public void onResponse(String response) {
}
});
File file = new File(Environment.getExternalStorageDirectory()+"/kwwl/abc.jpg");
HashMap<String, String> paramsMap = new HashMap<>();
paramsMap.put("title","title");
UrlHttpUtil.uploadFile("url", file, "image",UrlHttpUtil.FILE_TYPE_FILE, paramsMap, new CallBackUtil.CallBackString() {
@Override
public void onFailure(int code, String errorMessage) {
}
@Override
public void onResponse(String response) {
}
});
UrlHttpUtil.downloadFile("url", new CallBackUtil.CallBackFile("fileDir","fileName") {
@Override
public void onFailure(int code, String errorMessage) {
}
@Override
public void onProgress(float progress, long total) {
super.onProgress(progress, total);
}
@Override
public void onResponse(File response) {
}
});