博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
网络_Xutils
阅读量:4290 次
发布时间:2019-05-27

本文共 3189 字,大约阅读时间需要 10 分钟。

xUtils有四大模块:
数据库DbUtils,控件ViewUtils,网络ViewUtils,图片BitmapUtils
// get 提交
HttpUtils http = new HttpUtils();http.send(HttpRequest.HttpMethod.GET,    "http://www.lidroid.com",    new RequestCallBack
(){ @Override public void onLoading(long total, long current, boolean isUploading) { testTextView.setText(current + "/" + total); } @Override public void onSuccess(ResponseInfo
responseInfo) { textView.setText(responseInfo.result); } @Override public void onStart() { } @Override public void onFailure(HttpException error, String msg) { }});
// 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
() { @Override public void onStart() { testTextView.setText("conn..."); } @Override public void onLoading(long total, long current, boolean isUploading) { if (isUploading) { testTextView.setText("upload: " + current + "/" + total); } else { testTextView.setText("reply: " + current + "/" + total); } } @Override public void onSuccess(ResponseInfo
responseInfo) { testTextView.setText("reply: " + responseInfo.result); } @Override public void onFailure(HttpException error, String msg) { testTextView.setText(error.getExceptionCode() + ":" + msg); }});
// 下载文件
HttpUtils http = new HttpUtils();HttpHandler handler = http.download("http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip",    "/sdcard/httpcomponents-client-4.2.5-src.zip",    true, // 如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。    true, // 如果从请求返回信息中获取到文件名,下载完成后自动重命名。    new RequestCallBack
() { @Override public void onStart() { testTextView.setText("conn..."); } @Override public void onLoading(long total, long current, boolean isUploading) { testTextView.setText(current + "/" + total); } @Override public void onSuccess(ResponseInfo
responseInfo) { testTextView.setText("downloaded:" + responseInfo.result.getPath()); } @Override public void onFailure(HttpException error, String msg) { testTextView.setText(msg); }});...//调用cancel()方法停止下载handler.cancel();...

转载地址:http://rbegi.baihongyu.com/

你可能感兴趣的文章
javascript设计模式-链式编程(3)
查看>>
大型高并发与高可用缓存架构总结
查看>>
javascript设计模式-工厂模式(4)
查看>>
javascript设计模式-组合模式(6)
查看>>
javascript设计模式-门面模式(7)
查看>>
javascript设计模式-享元模式(10)
查看>>
javascript设计模式-代理模式(11)
查看>>
Executor相关源码分析
查看>>
react之setState解析
查看>>
elasticsearch7.3版本已经不需要额外安装中文分词插件了
查看>>
【重大好消息】elasticsearch 7.3版本已经可以免费使用x-pack就可以设置账号和密码了,让你的数据不再裸奔
查看>>
解决使用logstash中jdbc导入mysql中的数据到elasticsearch中tinyint类型被转成布尔型的问题的方法
查看>>
elasticsearch7.3版本环境搭建(一)elasticsearch安装和配置
查看>>
SEO基本功:站内优化的一些基本手段
查看>>
centos6系列和7系列如何对外开放80,3306端口号或者其他端口号
查看>>
为什么您宁愿吃生活的苦,也不愿吃学习的苦?为什么你不愿意去学习呢
查看>>
解决elasticsearch7.3版本安装过程中遇到的包括内存不够、线程不够等问题
查看>>
日常项目测试用例检查点(来自一线测试人员的吐血总结)
查看>>
网站建设之域名注册和域名备案
查看>>
解决bootstrap时间输入框总被浏览器记住的记录遮挡住的问题
查看>>