Android 利用an框架快速实现网络请求(含下载上传文件)
生活随笔
收集整理的這篇文章主要介紹了
Android 利用an框架快速实现网络请求(含下载上传文件)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
作者:Bgwan
鏈接:https://zhuanlan.zhihu.com/p/22573081
來源:知乎
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
鏈接:https://zhuanlan.zhihu.com/p/22573081
來源:知乎
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
an框架的網(wǎng)絡(luò)框架是完全基于Xutils3.0以上的,這里需要感謝前輩/wyouflf/xUtils3的無私奉獻(xiàn)。
Begin使用方法:
compile 'com.github.qydq:an-maven-base:0.0.9'DXUtils,XProgressCallBack和XCallBack是an框架提供的輔助類。
1.得到以Xml的形式返回的數(shù)據(jù)
private void getxml() {String url = "http://flash.weather.com.cn/wmaps/xml/china.xml";DXUtils.Get(url, null, new XCallBack<String>() {@Overridepublic 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();}}@Overridepublic void onError(Throwable ex, boolean isOnCallback) { super.onError(ex, isOnCallback);}}); }2.下載帶進(jìn)度的文件
private void downloadprogressfile() { //文件下載地址String url = "";//文件保存在本地的路徑String filepath = "";DXUtils.DownLoadFile(url, filepath, new XProgressCallBack<File>() {@Overridepublic void onSuccess(File result) { super.onSuccess(result);showToast("result:" + result.toString());}@Overridepublic 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>() { @Overridepublic void onSuccess(File result) { super.onSuccess(result);showToast("result:" + result.toString());}@Overridepublic void onError(Throwable ex, boolean isOnCallback) { super.onError(ex, isOnCallback);}}); }4.上傳文件
/*** 上傳文件(支持多文件上傳)*/ private void uploadfile() { //圖片上傳地址String url = "";Map<String, Object> map = new HashMap<>();//傳入自己的相應(yīng)參數(shù)//map.put(key, value);//map.put(key, value);DXUtils.UpLoadFile(url, map, new XCallBack<String>() {@Overridepublic void onSuccess(String result) { super.onSuccess(result);showToast("result:" + result);}@Overridepublic 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>() {@Overridepublic void onSuccess(PersonInfoBean result) { super.onSuccess(result);Log.e("result", result.toString());showToast("result:" + result.getResult());}@Overridepublic 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>() {@Overridepublic void onSuccess(PersonInfoBean result) { super.onSuccess(result);Log.e("result", result.toString());showToast("result,信息:" + result.getMsgid());}@Overridepublic void onError(Throwable ex, boolean isOnCallback) { super.onError(ex, isOnCallback);}}); }End轉(zhuǎn)載于:https://www.cnblogs.com/Free-Thinker/p/6419352.html
總結(jié)
以上是生活随笔為你收集整理的Android 利用an框架快速实现网络请求(含下载上传文件)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: String 堆内存和栈内存
- 下一篇: dynamic_cast, stati