-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Networking with the Volley Library
Volley is a library that makes networking for Android apps easier and most importantly, faster. Volley Library was announced by Ficus Kirkpatrick at Google I/O '13! It was first used by the Play Store team in Play Store Application and then they released it as an Open Source Library.
- Volley can pretty much do everything with that has to do with Networking in Android. It has some built-in Requests, such as JsonObjectRequest, JsonArrayRequest, StringRequest etc, based on the generic type of the Request class inside the Library.
- Volley automatically schedule all network requests. It means that Volley will be taking care of all the network requests your app executes for fetching response or image from web.
- Volley provides transparent disk and memory caching.
- Volley provides powerful cancellation request API. It means that you can cancel a single request or you can set blocks or scopes of requests to cancel.
- Volley provides powerful customization abilities.
- Volley provides debugging and tracing tools
-
First of all you have to download volley by cloning the project from
git clone https://android.googlesource.com/platform/frameworks/volley
-
After you have done this you just copy the com folder inside your package and start using Volley!
Volley has two Main Class files that you will have to deal with basically.
- RequestQueue
- Request (and any extension of it)
In your Activity's onCreate Method or in any other Java file you want you just create a new RequestQueue object like in the example below:
public YourActivity extends Activty{
private RequestQueue mRequestQueue;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen_layout);
mRequestQueue = Volley.newRequestQueue(this);
}
}
After this step you are ready to create your Request object or some extension of it (we are using StringRequest in the example for the sake of simplicity):
public YourActivity extends Activty{
private RequestQueue mRequestQueue;
private StringRequest mStringRequest_GET;
private StringRequest mStringRequest_POST;
private String URL = "your_url_here";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen_layout);
mRequestQueue = Volley.newRequestQueue(this);
buildRequestAndQueueIt();
}
private void buildRequestAndQueueIt(){
/*StringRequest using GET method*/
mStringRequest_GET = new StringRequest(
URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Handle Response from URL
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Do something else if request goes wrong
}
}
);
/*StringRequest using POST method*/
mStringRequest_POST = new StringRequest(
Method.POST,
URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Handle Response from URL
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Do something else if request goes wrong
}
}
);
// Optionally you can pass your own POST parameters by overriding getParams() Method
// and adding a Map<String,String> with your parameters and their names.
/*Finally add your Requests to the RequestQueue*/
mRequestQueue.add(mStringRequest_GET);
mRequestQueue.add(mStringRequest_POST);
}
}
And that's it you are done :) !
Of course you can replace StringRequest with every other type of Request you want or just create your own!
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.