4、 DbUtils的模块(点击进入)
现在开始学习HttpUtils,同样咋们还是以三部曲的方式来阐述这个问题:
一、添加权限、和一些注意事项
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
mHttpUtils = new HttpUtils();//最好整个应用一个实例
/**
* 给httputils配置各种参数
*/
void configUtils(){
//设置线程池数量
mHttpUtils.configRequestThreadPoolSize(4);
//设置请求重试次数
mHttpUtils.configRequestRetryCount(3);
//设置响应编码
mHttpUtils.configResponseTextCharset("utf-8");
//设置请求超时时间
mHttpUtils.configTimeout(30000);
}
三、正式使用httpUtils来操作数据
/**
* 发送get请求
*
*/
protected void sendGetRequest(){
MyAppliction.httpUtils.send(
HttpRequest.HttpMethod.GET,
"https://www.baidu.com/",
new RequestCallBack() {
@Override
public void onSuccess(ResponseInfo responseInfo) {
Log.i("sendGetRequest", responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
Log.i("sendGetRequest", msg);
}
});
}
/**
* 使用带参数的方式方式get请求
*/
protected void sendGetRuquest2(){
String strUrl = "http://op.juhe.cn/yi18/news/newscalss";
RequestParams params = new RequestParams();
NameValuePair pair = new BasicNameValuePair("key","c90b25c724fea7c5d6207e352fb03c63");
params.addQueryStringParameter(pair);
MyAppliction.httpUtils.send(HttpRequest.HttpMethod.GET,
strUrl,params,
new RequestCallBack() {
@Override
public void onSuccess(ResponseInfo responseInfo) {
Log.i("sendGetRuquest2",responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
Log.i("sendGetRuquest2",msg);
}
});
}
/**
* post请求
*/
protected void sendPostRequest(){
String strUrl = "http://op.juhe.cn/yi18/news/newscalss";
RequestParams params = new RequestParams();
NameValuePair pair = new BasicNameValuePair("key","c90b25c724fea7c5d6207e352fb03c63");
params.addQueryStringParameter(pair);
MyAppliction.httpUtils.send(HttpRequest.HttpMethod.POST,
strUrl, params,
new RequestCallBack() {
@Override
public void onSuccess(ResponseInfo responseInfo) {
Log.i("sendPostRequest","responseInfo "+responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
Log.i("sendPostRequest",msg);
}
});
}
/**
* 文件下载
*/
protected void downloadFile(){
String strUrl = "http://apache.fayea.com//httpcomponents/httpclient/binary/httpcomponents-client-4.5.1-bin.zip";//下载文件的地址
MyAppliction.httpUtils.download(strUrl,
"/sdcard/httpclient.zip",//下载的文件保存的地址
new RequestCallBack() {
@Override
public void onSuccess(ResponseInfo responseInfo) {
Log.i("downloadFile","ok");
}
@Override
public void onFailure(HttpException error, String msg) {
Log.i("downloadFile",msg);
}
//可以实时更新下载进度
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
String pross = current*100/total+"%";//计算下载进度
Log.i("downloadFile",pross);
}
}
);
}
/**
* 上传文件
*/
protected void uploadFiles(){
String strUrl = "http://10.2.160.190/upload.php";//上传的目标地址
RequestParams params = new RequestParams();//可以上传多个文件
params.addBodyParameter("files[0]",new File("sdcard/kunkun.jpg"));
params.addBodyParameter("files[1]",new File("sdcard/lastyear.jpg"));
MyAppliction.httpUtils.send(HttpRequest.HttpMethod.POST, strUrl, params,
new RequestCallBack() {
@Override
public void onSuccess(ResponseInfo responseInfo) {
Log.i("uploadFiles",responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
Log.i("uploadFiles","msg "+msg);
}
}
);
}