Android uses the Android-query Framework for Development (2)

Source: Internet
Author: User

Asynchronous Network:

1. add permissions:

2. Supported types

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 (){

@ 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 ();
}
}

});


The above form can also be written as follows, they are unconditional equivalent


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
}
}


Another example is to use XmlDom of AQuery to parse xml. If the XML is too large, use 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 ){
// Return a series of entry nodes and add them to the list
List Entries = xml. tags ("entry ");
List Titles = new ArrayList ();

String imageUrl = null;

For (XmlDom entry: entries ){
Titles. add (entry. text ("title"); // cyclically put the text with the first node as title into the title
ImageUrl = entry. tag ("content", "type", "image/jpeg "). attr ("src"); // you can specify content as the first node, type as the attribute, and image/jpeg as the src attribute to imageUri.
}

Aq. id (R. id. image). image (imageUrl );
}


4. If you want to specify the location where the file is saved, 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
     
      (){                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 (gson data uses Object Parsing in the document example). For details, see the document

6. Use Http Post (Multiple)

Privatevoid aync_multipart (){

                String url = "https://graph.facebook.com/me/photos";                Map
     
       params = new HashMap
      
       ();        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. Using ajax can easily achieve caching.
String url = "http://www.google.com"; // returns the cached copy of the last 15 minutes. If expire is-1, the content is updated immediately and long expire = 15*60*1000 is cached; aq. ajax (url, String. class, expire, new AjaxCallback
      
       
() {@ Override public void callback (String url, String html, AjaxStatus status) {showResult (html );}});
      
8. Invalid Cache
Public void callback (String url, JSONObject json, AjaxStatus status) {if (json! = Null) {if ("1 ". equals (json. optString ("status") {// do something} else {// The status is not cached. invalidate ();}}}
9. synchronous call: If ajax is called in a new thread, the sync method can block the thread until the ajax call is completed. If the sync method is used in the main thread, an Exception will occur.
String url = "http://www.google.com/uds/GnewsSearch?q=Obama&v=1.0";        AjaxCallback
      
        cb = new AjaxCallback
       
        ();           cb.url(url).type(JSONObject.class);                     aq.sync(cb);        JSONObject jo = cb.getResult();AjaxStatus status = cb.getStatus();
       
      

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.