Android 利用an框架快速实现网络请求(含下载上传文件)

作者:Bgwan
链接:https://zhuanlan.zhihu.com/p/22573081
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

an框架的网络框架是完全基于Xutils3.0以上的,这里需要感谢前辈/wyouflf/xUtils3的无私奉献。

Begin

使用方法:

compile 'com.github.qydq:an-maven-base:0.0.9'

DXUtils,XProgressCallBack和XCallBack是an框架提供的辅助类。

1.得到以Xml的形式返回的数据

private void getxml() {
String url = "http://flash.weather.com.cn/wmaps/xml/china.xml";
DXUtils.Get(url, null, new XCallBack<String>() { @Override
public void onSuccess(String xmlString) {
super.onSuccess(xmlString);
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xmlString));
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
String nodeName = xmlPullParser.getName();
if ("city".equals(nodeName)) {
String pName = xmlPullParser.getAttributeValue(0);
Log.e("TAG", "city is " + pName);
showToast("city is:" + pName + "weather is");
}
break;
}
eventType = xmlPullParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
} @Override
public void onError(Throwable ex, boolean isOnCallback) {
super.onError(ex, isOnCallback);
} });
}

2.下载带进度的文件

private void downloadprogressfile() {
//文件下载地址
String url = "";
//文件保存在本地的路径
String filepath = "";
DXUtils.DownLoadFile(url, filepath, new XProgressCallBack<File>() { @Override
public void onSuccess(File result) {
super.onSuccess(result);
showToast("result:" + result.toString());
} @Override
public void onError(Throwable ex, boolean isOnCallback) {
super.onError(ex, isOnCallback); } });
}

3.下载普通类型的文件

private void downloadfile() {
//文件下载地址
String url = "";
//文件保存在本地的路径
String filepath = "";
DXUtils.DownLoadFile(url, filepath, new XCallBack<File>() {
@Override
public void onSuccess(File result) {
super.onSuccess(result);
showToast("result:" + result.toString()); } @Override
public void onError(Throwable ex, boolean isOnCallback) {
super.onError(ex, isOnCallback); } });
}

4.上传文件

/**
* 上传文件(支持多文件上传)
*/
private void uploadfile() {
//图片上传地址
String url = "";
Map<String, Object> map = new HashMap<>();
//传入自己的相应参数
//map.put(key, value);
//map.put(key, value);
DXUtils.UpLoadFile(url, map, new XCallBack<String>() { @Override
public void onSuccess(String result) {
super.onSuccess(result);
showToast("result:" + result);
} @Override
public void onError(Throwable ex, boolean isOnCallback) {
super.onError(ex, isOnCallback);
} }); }

5.an框架提供的Get请求

private void get() {
String url = "http://api.k780.com:88/?app=idcard.get";
Map<String, String> map = new HashMap<>();
map.put("appkey", "10003");
map.put("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4");
map.put("format", "json");
map.put("idcard", "110101199001011114");
DXUtils.Get(url, map, new XCallBack<PersonInfoBean>() { @Override
public void onSuccess(PersonInfoBean result) {
super.onSuccess(result);
Log.e("result", result.toString());
showToast("result:" + result.getResult());
} @Override
public void onError(Throwable ex, boolean isOnCallback) {
super.onError(ex, isOnCallback); } });
}

6.an框架提供的Post请求

private void post() {
String url = "http://api.k780.com:88/?app=idcard.get";
Map<String, Object> map = new HashMap<>();
map.put("appkey", "10003");
map.put("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4");
map.put("format", "json");
map.put("idcard", "110101199001011114");
DXUtils.Post(url, map, new XCallBack<PersonInfoBean>() { @Override
public void onSuccess(PersonInfoBean result) {
super.onSuccess(result);
Log.e("result", result.toString());
showToast("result,信息:" + result.getMsgid());
} @Override
public void onError(Throwable ex, boolean isOnCallback) {
super.onError(ex, isOnCallback); }
});
} End
上一篇:菜鸟学SSH(五)——Struts2上传文件


下一篇:Java EE之通过表单上传文件