Android learning notes: Use of the Andorid network request framework Volley (I)

Source: Internet
Author: User

Android learning notes: Use of the Andorid network request framework Volley (I)

The Volley framework was released at the Google I/O 2013 conference. Volley was born from Google's network communication library on the Android platform. This framework enables faster, simpler, and more robust network communication. Volley has the following features:

I: easier and faster communication

Ll: Get and Post network requests and network images for efficient asynchronous Processing

III: You can sort the priorities of multiple network requests to cancel multiple network requests.

IV: The network request cache is linked with the Activity lifecycle. When the Activity is destroyed, the network request can be canceled to prevent memory leakage.

Volley is easy to use:

1. Download Volley's jar package or source code,

2. Get RequestQueue. Generally, we put it in the Application for initialization.

3. instantiate a Request object (StringRequest, JsonObjectRequest, JsonArrayRequest) 4. Add the Request to RequestQueue.
The first thing to learn is the basic StringRequest: homepage layout file: activity_main.xml.
Xmlns: tools = http://schemas.android.com/tools
Android: layout_width = match_parent
Android: layout_height = wrap_content
Android: orientation = vertical
Android: layout_margin = 10dp>
Android: id = @ + id/get_btn
Android: layout_width = match_parent
Android: layout_height = wrap_content
Android: gravity = center
Android: text = GET request
Android: textSize = 16sp
Android: layout_marginTop = 15dp/>
Android: id = @ + id/post_btn
Android: layout_width = match_parent
Android: layout_height = wrap_content
Android: gravity = center
Android: text = POST request
Android: textSize = 16sp/>

------------- Request operation ---------------------- public class MainActivity extends Activity implements OnClickListener {
Private Button getBtn, postBtn;


@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
GetBtn = (Button) findViewById (R. id. get_btn );
PostBtn = (Button) findViewById (R. id. post_btn );
GetBtn. setOnClickListener (this );
PostBtn. setOnClickListener (this );
}

@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. get_btn: // GET request to obtain data
GetStringRequest ();
Break;
Case R. id. post_btn: // POST request to obtain data
PostStringequest ();
Break;
}
}


Private void getStringRequest () {// use the GET method to request StringRequest using string data
String url = http://apis.juhe.cn/mobile/get? Phone = 134 *** 4320 & key = d6099881640aed5e0bacc058cfd4357b; // the key value is the api applied for on the aggregated data.
StringRequest strRequest = new StringRequest (Method. GET, url, new Listener (){
@ Override
Public void onResponse (String arg0) {// request successful
Toast. makeText (MainActivity. this, arg0, Toast. LENGTH_LONG). show ();
}
}, New ErrorListener (){
@ Override
Public void onErrorResponse (VolleyError arg0) {// request failed
Toast. makeText (MainActivity. this, arg0.toString (), Toast. LENGTH_LONG). show ();
}
});
StrRequest. setTag (getLDM );
MyApplication. getRequestQueues (). add (strRequest );
}

Private void postStringequest () {// use the POST method to request StringRequest using string data
String url = http://apis.juhe.cn/mobile/get ?;
StringRequest postRequest = new StringRequest (Method. POST, url, new Listener (){
@ Override
Public void onResponse (String arg0 ){
Toast. makeText (MainActivity. this, arg0, Toast. LENGTH_LONG). show ();
}
}, New ErrorListener (){

@ Override
Public void onErrorResponse (VolleyError arg0) {Toast. makeText (MainActivity. this, arg0.toString (), Toast. LENGTH_LONG). show ();
}
}){
@ Override
Protected Map GetParams () throws AuthFailureError {// The getparams method is used by Volley to transmit parameter information when POST is used to request data.
Map Map = new HashMap ();
Map. put (phone, 134 **** 4320 );
Map. put (key, d6099881640aed5e0bacc058cfd4357b );
Return map;
}
};
PostRequest. setTag (postLDM );
MyApplication. getRequestQueues (). add (postRequest );
}
}

 

 

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.