異步網絡:
1. 添加權限:<uses-permission android:name="android.permission.INTERNET" />
2. 支持的類型
JSONObjectJSONArray
String (HTML, XML)
XmlDom (XML parsing)
XmlPullParser (Large XML files)
byte array
User defined custom type (Transformer)
Bitmap
3. 以Json數據為例,注意,紅色部分是隨你請求的數據類型一起改變
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();
}
}
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();
}
}
});
上面的形式也可以寫成下面一樣,他們是無條件對等
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
}
}
// 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
}
}
再舉一個使用AQuery的XmlDom解析xml的例子,如果XML過大,使用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的結點,並把其add進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")); //循環把第一個結點為title的文本放進title
imageUrl = entry.tag("content", "type", "image/jpeg").attr("src");//把第一個結點為content,屬性為type,屬性值為image/jpeg的src屬性值賦予給imageUri
}
aq.id(R.id.image).image(imageUrl);
}
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的結點,並把其add進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")); //循環把第一個結點為title的文本放進title
imageUrl = entry.tag("content", "type", "image/jpeg").attr("src");//把第一個結點為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數據使用對象解析),詳細見文檔
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是很容易達到緩存的String url = "http://www.google.com";8. 使緩存無效
// 返回最近15分鍾內的緩存副本,如果expire為-1,內容將會立即更新且緩存
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);
}
});9. 同步調用: 如果ajax調用是在新開的線程,sync方法能夠阻塞線程,直到ajax調用完畢,如果sync方法用在主線程將會引起Exceptionpublic void callback(String url, JSONObject json, AjaxStatus status) {
if(json != null){
if("1".equals(json.optString("status"))){
//do something
}else{
// 不緩存
status.invalidate();
}
}
}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();
