2. 1. Introduction To Volley
1. Volley is an HTTP library that makes networking for Android
apps easier and most importantly, faster.
2. Volley is developed by Google & introduced during Google
I/O 2013.
3. Volley is available through the AOSP or GitHub.
3. Using Volley In Your Project
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-
v7:24.0.0'
compile 'com.android.volley:volley:1.0.0'
}
4. 2. Benefits of Volley
Automatic scheduling of network requests.
Multiple concurrent network connections.
Support for request prioritization.
Cancellation request API. You can cancel a single request, or
you can set blocks or scopes of requests to cancel.
Ease of customization, for example, for retry and backoff.
Strong ordering that makes it easy to correctly populate your UI
with data fetched asynchronously from the network.
Debugging and tracing tools.
12. 2. Sending a Simple Request
Add the Internet permission
Use newRequestQueue
Send a RequestQueue
Cancel a Request
13. 2.1 Add the Internet Permission
Add android.permission.INTERNET permission to app's
manifest
<uses-permission android:name="android.permission.INTERNET/>
14. 2.2 Use newRequestQueue
Volley provides a convenience method
Volley.newRequestQueue that sets up a RequestQueue for
you, using default values, and starts the queue.
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
15. 2.3 Send a Request
To send a request, you simply construct one and add it to
the RequestQueue object with add(StringRequest Object).
// Add the request to the RequestQueue.
queue.add(stringRequest);
18. 2.4 Cancel a Request
To cancel a request, call cancelAll()on your Request object.
We can call cancel a request in onStop() method of An
activtiy
mRequestQueue.cancelAll(Object Tag);
@Override
protected void onStop () {
super.onStop();
if (mRequestQueue != null) {
mRequestQueue.cancelAll(TAG);
}
}
20. Example (Contd.)
In your activity's onStop() method, cancel all requests that
have this tag.
21. 3. Making a Standard Request
Introduction to Standard Request
Request an Image
Use ImageRequest
Use ImageLoader and NetworkImageView
Request JSON
22. 3.1 Introduction to Standard
Request
StringRequest: Specify a URL and receive a raw string in
response.
ImageRequest: Specify a URL and receive an image in
response.
JsonObjectRequest and JsonArrayRequest (both
subclasses of JsonRequest): Specify a URL and get a
JSON object or array (respectively) in response.
23. 3.2 Request an Image
Volley offers the following classes for requesting images.
These classes layer on top of each other to offer different
levels of support for processing images:
ImageRequest—a canned request for getting an image at a
given URL and calling back with a decoded bitmap. It also
provides convenience features like specifying a size to resize
to.
ImageLoader—a helper class that handles loading and
caching images from remote URLs. ImageLoader is a an
orchestrator for large numbers of ImageRequests, for example
when putting multiple thumbnails in a ListView.
NetworkImageView—builds on ImageLoader and effectively
replaces ImageView for situations where your image is being
fetched over the network via URL. NetworkImageView also
manages canceling pending requests if the view is detached
from the hierarchy.
30. 4. Using Request JSON
Volley provides the following classes for JSON requests:
JsonArrayRequest—A request for retrieving a JSONArray
response body at a given URL.
JsonObjectRequest—A request for retrieving a JSONObject
response body at a given URL, allowing for an optional
JSONObject to be passed in as part of the request body.
Note: Both classes are based on the common base class
JsonRequest.
31. JSON Request
JsonObjectRequest request JsonObjectRequest(RequestMethod,
URL, null, new ResponseListener(), new ErrorListener());
Parameters passed into the constructor:
RequestMethod: (GET, POST, PUT, DELETE, etc.)
URL: String of the URL of the required object
JSONObject: An optional object posted with the request, null if there is no
object posted
ResponseListener: Response Listener, whose callback method will contain
the response
ErrorListener: A Response.ErrorListener whose callback method will
contain any problem with the request.
32. Example: Request JSON
Create A New Class (MySingleton)
MainActivity.java Class
main_activity.xml Class