Android之使用Android-query框架开发实战(二)
在上篇文章跟大家介绍了Android之使用Android-query框架开发实战(一),本文继续跟大家介绍有关Android-query框架。具体内容请看下文。
异步网络:
1.添加权限:<uses-permissionandroid:name="android.permission.INTERNET"/>
2.支持的类型
JSONObject
JSONArray
String(HTML,XML)
XmlDom(XMLparsing)
XmlPullParser(LargeXMLfiles)
bytearray
Userdefinedcustomtype(Transformer)
Bitmap
3.以Json数据为例,注意,红色部分是随你请求的数据类型一起改变
Stringurl="http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0"; aq.ajax(url,JSONObject.class,newAjaxCallback<JSONObject>(){ @Override publicvoidcallback(Stringurl,JSONObjectjson,AjaxStatusstatus){ if(json!=null){ //successfulajaxcall,showstatuscodeandjsoncontent Toast.makeText(aq.getContext(),status.getCode()+":"+json.toString(),Toast.LENGTH_LONG).show(); }else{ //ajaxerror,showerrorcode Toast.makeText(aq.getContext(),"Error:"+status.getCode(),Toast.LENGTH_LONG).show(); } } });
上面的形式也可以写成下面一样,他们是无条件对等
publicvoidasyncJson(){ //performaGooglesearchinjustafewlinesofcode Stringurl="http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0"; aq.ajax(url,JSONObject.class,this,"jsonCallback"); } publicvoidjsonCallback(Stringurl,JSONObjectjson,AjaxStatusstatus){ if(json!=null){ //successfulajaxcall }else{ //ajaxerror } }
再举一个使用AQuery的XmlDom解析xml的例子,如果XML过大,使用XMLPullParser
publicvoidxml_ajax(){ Stringurl="https://picasaweb.google.com/data/feed/base/featured?max-results=8"; aq.ajax(url,XmlDom.class,this,"picasaCb"); }
publicvoidpicasaCb(Stringurl,XmlDomxml,AjaxStatusstatus){ //返回一系列为entry的结点,并把其add进list List<XmlDom>entries=xml.tags("entry"); List<String>titles=newArrayList<String>(); StringimageUrl=null; for(XmlDomentry: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方法
Stringurl="https://picasaweb.google.com/data/feed/base/featured?max-results=16"; Fileext=Environment.getExternalStorageDirectory(); Filetarget=newFile(ext,"aquery/myfolder/photos.xml"); aq.progress(R.id.progress).download(url,target,newAjaxCallback<File>(){ publicvoidcallback(Stringurl,Filefile,AjaxStatusstatus){ if(file!=null){ showResult("File:"+file.length()+":"+file,status); }else{ showResult("Failed",status); } } });
5.自定义类型(文档例子是gson数据使用对象解析),详细见文档
6.使用HttpPost(Multiple)
privatevoidaync_multipart(){ Stringurl="https://graph.facebook.com/me/photos"; Map<String,Object>params=newHashMap<String,Object>(); params.put("message","Message"); //Simplyputabyte[]totheparams,AQuerywilldetectitandtreatitasamulti-partpost byte[]data=getImageData(); params.put("source",data); //Alternatively,putaFileorInputStreaminsteadofbyte[] //Filefile=getImageFile(); //params.put("source",file); AQueryaq=newAQuery(getApplicationContext()); aq.auth(handle).ajax(url,params,JSONObject.class,this,"photoCb"); }
7.使用ajax是很容易达到缓存的
Stringurl="http://www.google.com"; //返回最近15分钟内的缓存副本,如果expire为-1,内容将会立即更新且缓存 longexpire=15*60*1000; aq.ajax(url,String.class,expire,newAjaxCallback<String>(){ @Override publicvoidcallback(Stringurl,Stringhtml,AjaxStatusstatus){ showResult(html); } });
8.使缓存无效
publicvoidcallback(Stringurl,JSONObjectjson,AjaxStatusstatus){ if(json!=null){ if("1".equals(json.optString("status"))){ //dosomething }else{ //不缓存 status.invalidate(); } } }
9.同步调用:如果ajax调用是在新开的线程,sync方法能够阻塞线程,直到ajax调用完毕,如果sync方法用在主线程将会引起Exception
Stringurl="http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0"; AjaxCallback<JSONObject>cb=newAjaxCallback<JSONObject>(); cb.url(url).type(JSONObject.class); aq.sync(cb); JSONObjectjo=cb.getResult(); AjaxStatusstatus=cb.getStatus();
以上就是小小编跟大家就介绍的Android之使用Android-query框架开发实战(二),希望大家喜欢。