The volley framework was released at the Google I/O 2013 conference . Volley is the birth of Google's network Communications library on the Android platform, which makes network communication faster, simpler and more robust. The volley features are:
Ⅰ: Easier and faster communication
Ll:get,post network requests and network images for efficient asynchronous processing
III: Multiple network requests can be prioritized to cancel operations at the level of multiple levels
IV: Network request caching and interaction with the activity lifecycle, when the activity is destroyed, the network request can be canceled operation, to prevent memory leaks.
The use of volley is simple:
1, download volley jar package or source code, GitHub address: Https://github.com/stormzhang/AndroidVolley
2, get requestqueue, generally we put it in application to initialize the operation
3, instantiate a Request object (Stringrequest,jsonobjectrequest,jsonarrayrequest) 4, add the request to Requestqueue.
The first thing to learn is the most basic stringrequest: Main Page layout file: Activity_main.xml
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical"
android:layout_margin= "10DP" >
<button
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"/>
<button
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"/>
</LinearLayout>
-------------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 for data
Getstringrequest ();
Break
Case r.id.post_btn://POST request fetch data
Poststringequest ();
Break
}
}
private void Getstringrequest () {//Use the Get method to request stringrequest with string data
String url = "http://apis.juhe.cn/mobile/get?phone=134****4320&key=d6099881640aed5e0bacc058cfd4357b";// The key value is the API requested on the aggregated data
Stringrequest strrequest = new Stringrequest (method.get, URL, new listener<string> () {
@Override
public void Onresponse (String arg0) {//Request succeeded
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 with string data
String url = "Http://apis.juhe.cn/mobile/get?";
Stringrequest postrequest = new Stringrequest (method.post, URL, new listener<string> () {
@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<string, string> Getparams () throws Authfailureerror {//Getparams method is to pass parameter information when using POST request data in volley
map<string, string> map = new hashmap<string, string> ();
Map.put ("Phone", "134****4320");
Map.put ("Key", "d6099881640aed5e0bacc058cfd4357b");
return map;
}
};
Postrequest.settag ("POSTLDM");
Myapplication.getrequestqueues (). Add (Postrequest);
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Learning Note: Andorid Network request framework volley use (top)