xutils网络框架的最简单使用
来源:互联网 发布:标准件网络销售好学吗 编辑:程序博客网 时间:2024/05/20 06:07
小菜鸟一枚,公司有些需求是也在学习中,希望大神提出宝贵意见,谢谢。
首先是导入依赖包:
compile 'org.xutils:xutils:3.5.0'
接下来是对于网络接口地址的初始化:public class XHttpUrL { public static final String FATEHER_URL = "-----------接口地址--------";
}
对于网络接口的调用
public class XHttp { //utf-8格式转换(进行特殊格式转换调用的方法) public static String toUtf8(String str) { String result = null; try { result = URLEncoder.encode(str, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }public static void getFather(String node, Callback.CommonCallback<String> listener) {////根据后台确定是否需要转换 //不用转换的方法 x.http().post(new RequestParams(XHttpUrL.FATEHER_URL + node), listener);//用转换的方法}}x.http().post(new RequestParams(XHttpUrL.FATEHER_URL + toutf8(node)), listener);
//在方法中调用
首先是在oncreate方法中进行初始化,不然容易空指针x.Ext.init(getApplication());
然后直接开始调用需要的方法即可
列出几个例子:::::
普通get方法
HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.GET,
"http://www.lidroid.com",
new RequestCallBack<String>(){
@Override
public void onLoading(long total, long current, boolean isUploading) {
testTextView.setText(current + "/" + total);
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
textView.setText(responseInfo.result);
}
@Override
public void onStart() {
}
@Override
public void onFailure(HttpException error, String msg) {
}
});
使用HttpUtils上传文件 或者 提交数据 到服务器(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>() {
@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<String> responseInfo) {
testTextView.setText("reply: " + responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
testTextView.setText(error.getExceptionCode() + ":" + msg);
}
});
阅读全文
0 0
- xutils网络框架的最简单使用
- Xutils框架的使用
- XUtils框架的使用
- Xutils框架的使用
- xUtils框架的使用
- Android最火框架XUtils的使用详解
- xUtils的简单使用
- XUtils的简单使用
- xUtils的简单使用
- 使用网络XML,pull解析xUtils框架
- android okGo、okHttp、XUtils网络框架使用
- android xutils框架的使用
- xUtils框架的使用详解
- XUtils的网络请求使用
- XUtils学习(最火爆的框架)
- Orm框架之XUtils简单使用
- XUtils数据库的简单使用;
- xutils 3的简单使用
- Java远程通讯技术及原理分析
- python里None 表示False吗? (我是新手)
- can控制器SJA1000详解
- CoreDom的增删改查
- Golang 在数据库Replication场景下的应用及GC算法
- xutils网络框架的最简单使用
- Linux的开机过程略解读
- java定时器,Spring定时器和Quartz定时器
- 解决动态生成input无法触发click事件的问题
- Oracle程序包
- 版本控制最好用Git和GitHub,SVN其实不好用
- Dual Core CPU POJ
- PAT甲级真题及训练集(3)--1002. A+B for Polynomials (25)
- java Quartz定时器任务与Spring task定时的几种实现,