Android之使用Android-query框架进行开发(二)

转自: http://www.cnblogs.com/lee0oo0/archive/2012/10/25/2739132.html


Android之使用Android-query框架进行开发(二)

异步网络:

1. 添加权限:<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数据为例,注意,红色部分是随你请求的数据类型一起改变

复制代码
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";
        
        aq.ajax(url,  JSONObject. classnew 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. classthis, "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. classthis, "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";

// 返回最近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);
    }
        
});
8. 使缓存无效
public void callback(String url, JSONObject json, AjaxStatus status) {
    
        if(json != null){
                if("1".equals(json.optString("status"))){
                        //do something
                }else{
                        // 不缓存
                        status.invalidate();
                }
        }
}
9. 同步调用: 如果ajax调用是在新开的线程,sync方法能够阻塞线程,直到ajax调用完毕,如果sync方法用在主线程将会引起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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值