In the previous article with you introduced the use of Android Android-query Framework Development Combat (i), this article continues to introduce the Android-query framework. Please see below for details.
Asynchronous Network:
1. Add Permissions: <uses-permission android:name= "Android.permission.INTERNET"/>
2. Types of support
Jsonobject
Jsonarray
String (HTML, XML)
XMLDOM (XML parsing)
Xmlpullparser (Large XML files)
byte array
User defined custom type (Transformer)
Bitmap
3. Take JSON data as an example, note that the red part is changed along with the data type you requested
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 con Tent
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 ();
}
The above form can also be written as follows, they are unconditional equivalence
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 cal L
}else{
//ajax error
}
Give me an example of parsing XML using Aquery xmldom, if the XML is too large, use the 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) {
//Returns a series of entry nodes and add them to the 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"));//Loop The text of the first node as title into title
IMAGEURL = Entry.tag ("Content", "type", "Image/jpeg"). attr ("src");//The first node is content, the attribute is type, and the value of the attribute is image/jpeg with the SRC attribute value given to Imageuri
}
Aq.id (r.id.image). Image (IMAGEURL);
4. If you want to specify where to save the file, use the download method
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. Custom type (document example is Gson data using object resolution), see documents in detail
6. Use 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'll detect it and treat it as a multi-part post
byte[] data = Getimagedat A ();
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. Using AJAX is very easy to reach the cache
String url = "http://www.google.com";
Returns the cached copy within the last 15 minutes, and if expire is-1, the content is immediately updated and cached
long expire = * 1000;
Aq.ajax (URL, string.class, expire, new ajaxcallback<string> () {
@Override public
void Callback (String URL, String html, ajaxstatus status) {
showresult (HTML);
}
});
8. Invalidate the cache
public void callback (String URL, jsonobject json, ajaxstatus status) {
if (JSON!= null) {
if ("1". Equals ( Json.optstring ("status")) {
//do something
}else{
//Do not cache
status.invalidate ();
}}}
9. Synchronous call: If the AJAX call is on the newly opened thread, the Sync method can block the thread until the AJAX call completes, and if the sync method is used in the main thread, it will cause 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 ();
The above is a small series with everyone on the introduction of the use of Android Android-query framework to develop Combat (ii), I hope you like.