随时随地技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

摘自:http://blog.csdn.net/rain_butterfly/article/details/37812371

private void get() {HttpUtils http = new HttpUtils();http.send(HttpRequest.HttpMethod.GET, "http://www.lidroid.com",new RequestCallBack<String>() {@Overridepublic void onLoading(long total, long current,boolean isUploading) {showText.setText(current + "/" + total);}@Overridepublic void onSuccess(ResponseInfo<String> responseInfo) {showText.setText(responseInfo.result);}@Overridepublic void onStart() {}@Overridepublic void onFailure(HttpException error, String msg) {}});}
private void post() {RequestParams params = new RequestParams();params.addHeader("name", "value");params.addQueryStringParameter("name", "value");// 只包含字符串参数时默认使用BodyParamsEntity,// 类似于UrlEncodedFormEntity("application/x-www-form-urlencoded")。params.addBodyParameter("name", "value");// 加入文件参数后默认使用MultipartEntity("multipart/form-data"),// 如需"multipart/related",xUtils中提供的MultipartEntity支持设置subType为"related"。// 使用params.setBodyEntity(httpEntity)可设置更多类型的HttpEntity(如:// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。// 例如发送json参数:params.setBodyEntity(new StringEntity(jsonStr,charset));params.addBodyParameter("file", new File("path"));HttpUtils http = new HttpUtils();http.send(HttpRequest.HttpMethod.POST, "uploadUrl....", params,new RequestCallBack<String>() {@Overridepublic void onStart() {showText.setText("conn...");}@Overridepublic void onLoading(long total, long current,boolean isUploading) {if (isUploading) {showText.setText("upload: " + current + "/" + total);} else {showText.setText("reply: " + current + "/" + total);}}@Overridepublic void onSuccess(ResponseInfo<String> responseInfo) {showText.setText("reply: " + responseInfo.result);}@Overridepublic void onFailure(HttpException error, String msg) {showText.setText(error.getExceptionCode() + ":" + msg);}});}
private void downLoad(String url, String localPath) {HttpUtils http = new HttpUtils();HttpHandler handler = http.download(url, localPath, true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。new RequestCallBack<File>() {@Overridepublic void onStart() {showText.setText("conn...");}@Overridepublic void onLoading(long total, long current,boolean isUploading) {showText.setText(current + "/" + total);}@Overridepublic void onSuccess(ResponseInfo<File> responseInfo) {showText.setText("downloaded:"+ responseInfo.result.getPath());}@Overridepublic void onFailure(HttpException error, String msg) {showText.setText(msg);}});// 调用cancel()方法停止下载
//      handler.cancel();}

//封装的get请求工具类

package com.home.testxutils;import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;public class XUtilsGet {// 自动实现异步处理,自己不用处理public void getJson(String url, RequestParams params,final IOAuthCallBack iOAuthCallBack) {HttpUtils http = new HttpUtils();http.configCurrentHttpCacheExpiry(1000 * 10);// 设置超时时间http.send(HttpMethod.GET, url, params, new RequestCallBack<String>() {// 接口回调@Overridepublic void onFailure(HttpException arg0, String arg1) {}@Overridepublic void onSuccess(ResponseInfo<String> info) {iOAuthCallBack.getIOAuthCallBack(info.result);// 利用接口回调数据传输}});}public void getCataJson(int cityId, IOAuthCallBack iOAuthCallBack) {// 外部接口函数String url = "http://xxxxxxxxxx";RequestParams params = new RequestParams();params.addQueryStringParameter("currentCityId", cityId + "");getJson(url, params, iOAuthCallBack);}
}

封装的post工具类:

public class xUtilsPost {//自动实现异步处理  public void doPost(String url, RequestParams params,  final IOAuthCallBack iOAuthCallBack) {  HttpUtils http = new HttpUtils();  http.configCurrentHttpCacheExpiry(1000 * 10);  http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {  @Override  public void onFailure(HttpException arg0, String arg1) {  // TODO Auto-generated method stub  }  @Override  public void onSuccess(ResponseInfo<String> info) {  // TODO Auto-generated method stub  iOAuthCallBack.getIOAuthCallBack(info.result);  }  });  }  public void doPostLogin(int cityId, IOAuthCallBack iOAuthCallBack) {  String url = "http://xxxxxxxxxxxx";  RequestParams params = new RequestParams();  params.addBodyParameter("currentCityId", cityId + "");  params.addBodyParameter("path", "/apps/postCatch");  doPost(url, params, iOAuthCallBack);  }
}  

Xutils之HttpUtils使用相关推荐

  1. XUtils之HttpUtils

    HttpUtils请求网络数据 import java.util.List; import com.example.week1_httputils.Car.Carmsg; import com.goo ...

  2. 网络下载-xUtils,HttpUtils

    xUtils有四大模块: 数据库DbUtils,控件ViewUtils,网络ViewUtils,图片BitmapUtils // get 提交 HttpUtils http = new HttpUti ...

  3. Android应用开发:网络编程-2

    网络编程 Java基础:网络编程 Uri.URL.UriMatcher.ContentUris详解 Android应用开发:网络编程1 Android应用开发:网络编程2 1. 使用HttpClien ...

  4. 智慧北京02_初步ui框架_ 主界面_viewPager事件_xUtils_slidingMenu_网络缓存_数据传递...

    智慧北京02_初步ui框架_ 主界面_viewPager事件_xUtils_slidingMenu_网络缓存_数据传递 1.使用Fragment搭建ui框架 参考分析图 1.1,Fragment生命周 ...

  5. 入门android开发

    调节调试窗口字体: https://blog.csdn.net/qq_32452623/article/details/52403725 1.单元测试(未代码验证) 报出如下错误日志打印: java. ...

  6. Android快速开发之appBase——(6).HttpReq和APICloudSDK

    转载请注明本文出自JFlex的博客http://blog.csdn.net/jflex/article/details/46462077,请尊重他人的辛勤劳动成果,谢谢! Android快速开发之ap ...

  7. android 请求https接口

    随着互联网的强大,网络安全的地位也逐步增加.以前的http请求已经不在安全,据说ios2017年1月份开始请求https. 所有,身为android开发人员也应该会加载https请求.直接上干活 1. ...

  8. Android开源项目xUtils HttpUtils模块分析

    使用HttpUtils 只需要new 出一个实例便可. public class HttpFragment extends Fragment {     // 同步请求 必须在异步块儿中执行     ...

  9. android:java.lang.NoClassDefFoundError: com.lidroid.xutils.HttpUtils 异常的解决

    做项目练习的时候突然冒出了这个问题,而且前提是一个和xUtils毫不相关的业务,之前xUtils页面早就写好了,都没有问题,那么显然,这个和xUtils没有任何关系. 上网查了不少说明,解决办法都不太 ...

最新文章

  1. 西门子PLC学习笔记二-(工作记录)
  2. javascript菜鸟学习20170113
  3. c语言make编译器,cmake 指定编译器
  4. java 动态编译_老生常谈Java动态编译(必看篇)
  5. 实现Flex的TextArea文本中关键字的高亮显示
  6. WCF 第十二章 对等网 使用自定义绑定实现消息定向
  7. 兄弟HL-1118加粉清零
  8. 码神之路博客项目部署
  9. win10计算机记录,Win10新版计算器用法介绍
  10. union和union all哪个效率高
  11. (光滑样条)Smoothing spline的数学推导
  12. 机器学习系列(一), 监督学习和无监督学习
  13. 系统对接方案_钉钉报销单对接财务系统解决方案
  14. android小电脑,废旧笔记本电脑纯手工改装“安卓小电脑”,太赞了!
  15. 网易云音乐android面试题,# 每日一道面试题 # 如何测试网易云音乐 app 语音识别功能?...
  16. 学会python语法后的第一个爬虫
  17. 单片机音频节奏灯_如何用单片机做出用音乐节奏来控制LED灯?
  18. Android(国际化)多语言的实现和切换
  19. Java JDBC连接SQL Server2005错误:通过端口 1433 连接到主机 localhost 的 TCP/IP 连接失败(转载)...
  20. 强烈推荐这11个Python开源项目,非常值得入门学习(从入门到Python高级开发)

热门文章

  1. 小白学机器人伺服电机模块
  2. ALicoin:真正基于社区共识自治的数字资产交易平台
  3. Yii2.0 时间日期插件之yii2-timepicker
  4. python基础-变量,变量类型,字符串str,元组tuple,列表list,字典dict操作详解(超详细)
  5. NEC成功完成对Avaloq的收购
  6. python每天进步一点点
  7. java 避免gc_Java GC机制
  8. [新手篇] 深度优先搜索求解数字华容道
  9. 金山词霸2007专业版 破解补丁
  10. Mysql 远程登录设置