Android之使用Android-query框架開(kāi)發(fā)實(shí)戰(zhàn)(二)
在上篇文章跟大家介紹了Android之使用Android-query框架開(kāi)發(fā)實(shí)戰(zhàn)(一),本文繼續(xù)跟大家介紹有關(guān)Android-query框架。具體內(nèi)容請(qǐng)看下文。
異步網(wǎng)絡(luò):
1. 添加權(quán)限:<uses-permission android:name="android.permission.INTERNET" />
2. 支持的類型
JSONObject
JSONArray
String (HTML, XML)
XmlDom (XML parsing)
XmlPullParser (Large XML files)
byte array
User defined custom type (Transformer)
Bitmap
3. 以Json數(shù)據(jù)為例,注意,紅色部分是隨你請(qǐng)求的數(shù)據(jù)類型一起改變
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
aq.ajax(url, JSONObject.class, new AjaxCallback<JSONObject>() {
@Override
public void callback(String url, JSONObject json, AjaxStatus status) {
if(json != null){
//successful ajax call, show status code and json content
Toast.makeText(aq.getContext(), status.getCode() + ":" + json.toString(), Toast.LENGTH_LONG).show();
}else{
//ajax error, show error code
Toast.makeText(aq.getContext(), "Error:" + status.getCode(), Toast.LENGTH_LONG).show();
}
}
});
上面的形式也可以寫(xiě)成下面一樣,他們是無(wú)條件對(duì)等
public void asyncJson(){
//perform a Google search in just a few lines of code
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
aq.ajax(url, JSONObject.class, this, "jsonCallback");
}
public void jsonCallback(String url, JSONObject json, AjaxStatus status){
if(json != null){
//successful ajax call
}else{
//ajax error
}
}
再舉一個(gè)使用AQuery的XmlDom解析xml的例子,如果XML過(guò)大,使用XMLPullParser
public void xml_ajax(){
String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=8";
aq.ajax(url, XmlDom.class, this, "picasaCb");
}
public void picasaCb(String url, XmlDom xml, AjaxStatus status){
// 返回一系列為entry的結(jié)點(diǎn),并把其add進(jìn)list
List<XmlDom> entries = xml.tags("entry");
List<String> titles = new ArrayList<String>();
String imageUrl = null;
for(XmlDom entry: entries){
titles.add(entry.text("title")); //循環(huán)把第一個(gè)結(jié)點(diǎn)為title的文本放進(jìn)title
imageUrl = entry.tag("content", "type", "image/jpeg").attr("src");//把第一個(gè)結(jié)點(diǎn)為content,屬性為type,屬性值為image/jpeg的src屬性值賦予給imageUri
}
aq.id(R.id.image).image(imageUrl);
}
4. 如果你想指定保存文件的位置,使用download方法
String url = "https://picasaweb.google.com/data/feed/base/featured?max-results=16";
File ext = Environment.getExternalStorageDirectory();
File target = new File(ext, "aquery/myfolder/photos.xml");
aq.progress(R.id.progress).download(url, target, new AjaxCallback<File>(){
public void callback(String url, File file, AjaxStatus status) {
if(file != null){
showResult("File:" + file.length() + ":" + file, status);
}else{
showResult("Failed", status);
}
}
});
5. 自定義類型(文檔例子是gson數(shù)據(jù)使用對(duì)象解析),詳細(xì)見(jiàn)文檔
6. 使用Http Post (Multiple)
private void aync_multipart(){
String url = "https://graph.facebook.com/me/photos";
Map<String, Object> params = new HashMap<String, Object>();
params.put("message", "Message");
//Simply put a byte[] to the params, AQuery will detect it and treat it as a multi-part post
byte[] data = getImageData();
params.put("source", data);
//Alternatively, put a File or InputStream instead of byte[]
//File file = getImageFile();
//params.put("source", file);
AQuery aq = new AQuery(getApplicationContext());
aq.auth(handle).ajax(url, params, JSONObject.class, this, "photoCb");
}
7. 使用ajax是很容易達(dá)到緩存的
String url = "http://www.google.com";
// 返回最近15分鐘內(nèi)的緩存副本,如果expire為-1,內(nèi)容將會(huì)立即更新且緩存
long expire = 15 * 60 * 1000;
aq.ajax(url, String.class, expire, new AjaxCallback<String>() {
@Override
public void callback(String url, String html, AjaxStatus status) {
showResult(html);
}
});
8. 使緩存無(wú)效
public void callback(String url, JSONObject json, AjaxStatus status) {
if(json != null){
if("1".equals(json.optString("status"))){
//do something
}else{
// 不緩存
status.invalidate();
}
}
}
9. 同步調(diào)用:如果ajax調(diào)用是在新開(kāi)的線程,sync方法能夠阻塞線程,直到ajax調(diào)用完畢,如果sync方法用在主線程將會(huì)引起Exception
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0"; AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>(); cb.url(url).type(JSONObject.class); aq.sync(cb); JSONObject jo = cb.getResult(); AjaxStatus status = cb.getStatus();
以上就是小小編跟大家就介紹的Android之使用Android-query框架開(kāi)發(fā)實(shí)戰(zhàn)(二),希望大家喜歡。
- 六款值得推薦的android(安卓)開(kāi)源框架簡(jiǎn)介
- Android Retrofit 2.0框架上傳圖片解決方案
- 簡(jiǎn)略分析Android的Retrofit應(yīng)用開(kāi)發(fā)框架源碼
- 舉例講解Android應(yīng)用開(kāi)發(fā)中OTTO框架的基本使用
- Android通用流行框架大全【整理】
- Android中XUtils3框架使用方法詳解(一)
- Android AndBase框架使用封裝好的函數(shù)完成Http請(qǐng)求(三)
- Android最基本的異步網(wǎng)絡(luò)請(qǐng)求框架
- Android學(xué)習(xí)之Flux架構(gòu)入門(mén)
相關(guān)文章
Android編程簡(jiǎn)單實(shí)現(xiàn)ImageView點(diǎn)擊時(shí)背景圖修改的方法
這篇文章主要介紹了Android編程簡(jiǎn)單實(shí)現(xiàn)ImageView點(diǎn)擊時(shí)背景圖修改的方法,涉及Android針對(duì)背景圖相關(guān)屬性設(shè)置的操作技巧,需要的朋友可以參考下2015-12-12
詳解Android Studio中Git的配置及協(xié)同開(kāi)發(fā)
這篇文章主要介紹了詳解Android Studio中Git的配置及協(xié)同開(kāi)發(fā),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Android常見(jiàn)的幾種內(nèi)存泄漏小結(jié)
本篇文章主要介紹了Android常見(jiàn)的幾種內(nèi)存泄漏小結(jié)。詳細(xì)的介紹了內(nèi)存泄漏的原因及影響和解決方法,有興趣的可以了解一下。2017-03-03
Android實(shí)現(xiàn)閱讀進(jìn)度記憶功能
這篇文章主要介紹了Android實(shí)現(xiàn)閱讀進(jìn)度記憶功能,Android控件WebView實(shí)現(xiàn)保存閱讀進(jìn)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android安裝apk文件并適配Android 7.0詳解
這篇文章主要介紹了Android安裝apk文件并適配Android 7.0詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果
這篇文章主要介紹了Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果,實(shí)現(xiàn)思路是先分別實(shí)現(xiàn)閃屏、注冊(cè)界面、登錄界面的活動(dòng),再用Intent將相關(guān)的活動(dòng)連接起來(lái),實(shí)現(xiàn)不同活動(dòng)之間的跳轉(zhuǎn),對(duì)android 實(shí)現(xiàn)閃屏和界面切換感興趣的朋友一起看看吧2016-11-11
android實(shí)現(xiàn)倒計(jì)時(shí)功能的方法
這篇文章主要為大家詳細(xì)介紹了兩種android實(shí)現(xiàn)倒計(jì)時(shí)功能的方法,具有一定的實(shí)用性,感興趣的小伙伴們可以參考一下2016-08-08

