45
Transmitting Network Data Using Volley 14/September/2016 – 20/September/2016 Prepared by: Ms. Sokngim Sa

Transmitting network data using volley(14 09-16)

Embed Size (px)

Citation preview

Page 1: Transmitting network data using volley(14 09-16)

Transmitting Network Data Using Volley14/September/2016 – 20/September/2016 Prepared by: Ms. Sokngim Sa

Page 2: Transmitting network data using volley(14 09-16)

Content

1. Introduction Volley2. Benefit of Volley3. Sending a Simple Request4. Setting Up a RequestQueue5. Using ImageLoader6. Using Request JSON7. Implementing a Custom Request

Using RequestString

Page 3: Transmitting network data using volley(14 09-16)

1. Introduction VolleyVolley is an HTTP library that makes networking

for Android apps easier and most importantly, faster.

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

Page 4: Transmitting network data using volley(14 09-16)

2. Benefit of Volley Automatic scheduling of network requests. Multiple concurrent network connections. Transparent disk and memory response caching with

standard HTTP cache coherence. 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.

Page 5: Transmitting network data using volley(14 09-16)

3. Sending a Simple Request Add the Internet permission Use newRequestQueue Send a RequestQueue Cancel a Request

Page 6: Transmitting network data using volley(14 09-16)

2.1 Add the Internet Permission Add android.permission.INTERNET permission to

app's manifest

<uses-permission android:name="android.permission.INTERNET/>

Page 7: Transmitting network data using volley(14 09-16)

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);

Page 8: Transmitting network data using volley(14 09-16)

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);

Page 9: Transmitting network data using volley(14 09-16)

Processing Send a Request

Page 10: Transmitting network data using volley(14 09-16)

Example of RequestQueue and Send Request

Page 11: Transmitting network data using volley(14 09-16)

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);

@Overrideprotected void onStop () { super.onStop(); if (mRequestQueue != null) { mRequestQueue.cancelAll(TAG); }}

Page 12: Transmitting network data using volley(14 09-16)

Example

Define your tag and add it to your requests.

Page 13: Transmitting network data using volley(14 09-16)

Example (Con)

In your activity's onStop() method, cancel all requests that have this tag.

Page 14: Transmitting network data using volley(14 09-16)

3. Setting up a RequestQueue Set Up a Network and Cache Use a Singleton Pattern

Page 15: Transmitting network data using volley(14 09-16)

3.1 Set Up a Network and Cache A RequestQueue needs two things to do its job: a

network to perform transport of the requests, and a cache to handle caching.

There are standard implementations of these available in the Volley toolbox: DiskBasedCache provides a one-file-per-response

cache with an in-memory index BasicNetwork provides a network transport based on

your preferred HTTP client. BasicNetwork is Volley's default network

implementation. A BasicNetwork must be initialized with the HTTP client your app is using to connect to the network. Typically this is an HttpURLConnection.

Page 16: Transmitting network data using volley(14 09-16)

Example

Page 17: Transmitting network data using volley(14 09-16)

Note

If you just need to make a one-time request and don't want to leave the thread pool around, you can create the RequestQueue wherever you need it and call stop() on the RequestQueue once your response or error has come back.

using the Volley.newRequestQueue() method described in Sending a Simple Request.

But the more common use case is to create the RequestQueue as a singleton to keep it running for the lifetime of your app.

Page 18: Transmitting network data using volley(14 09-16)

3.2 Use a Singleton Pattern

If your application makes constant use of the network, it's probably most efficient to set up a single instance of RequestQueue that will last the lifetime of your app. The recommended approach is to implement a singleton class that encapsulates RequestQueue and other Volley functionality.

Another approach is to subclass Application and set up the RequestQueue in Application.onCreate(). A key concept is that the RequestQueue must be instantiated with the Application context, not an Activity context. This ensures that the RequestQueue will last for the lifetime of your app, instead of being recreated every time the activity is recreated (for example, when the user rotates the device).

Page 19: Transmitting network data using volley(14 09-16)

Create Singleton Class

Page 20: Transmitting network data using volley(14 09-16)

In MainActivity

Page 21: Transmitting network data using volley(14 09-16)

4. Making a Standard Request Introduction to Standard Request Request an Image Use ImageRequest Use ImageLoader and NetworkImageView Request JSON

Page 22: Transmitting network data using volley(14 09-16)

4.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.

Page 23: Transmitting network data using volley(14 09-16)

4.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.

Page 24: Transmitting network data using volley(14 09-16)

4.3 Use ImageRequest

Create Singleton Class MainActivity.java Main_activity.xml

Page 25: Transmitting network data using volley(14 09-16)

4.3.1 Create Singleton Class

Page 26: Transmitting network data using volley(14 09-16)

4.3.1 Create Singleton Class (Con)

Page 27: Transmitting network data using volley(14 09-16)

4.3.2 main_activity.xml

Page 28: Transmitting network data using volley(14 09-16)

4.3.3 MainActivity.java

Page 29: Transmitting network data using volley(14 09-16)

Output

Page 30: Transmitting network data using volley(14 09-16)

4.4 Using ImageLoader

Create Singleton class main_activity.xml MainActivity.java

Page 31: Transmitting network data using volley(14 09-16)

4.4.1 Create Singleton Class

Page 32: Transmitting network data using volley(14 09-16)

4.4.1 Create Singleton Class (Con)

Page 33: Transmitting network data using volley(14 09-16)

4.4.2 main_activity.xml

Page 34: Transmitting network data using volley(14 09-16)

4.4.3 MainActivity.java

Page 35: Transmitting network data using volley(14 09-16)

5. 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.

Page 36: Transmitting network data using volley(14 09-16)

Example: Request JSON

Create Singleton Class MainActivity.java Class main_activity.xml Class

Page 37: Transmitting network data using volley(14 09-16)

Create Singleton Class

Page 38: Transmitting network data using volley(14 09-16)

MainActivity.java

Page 39: Transmitting network data using volley(14 09-16)

main_activity.xml

Page 40: Transmitting network data using volley(14 09-16)

6. Implementing a Custom Request Most requests have ready-to-use implementations in

the toolbox; if your response is a string, image, or JSON, you probably won't need to implement a custom Request.

For cases where you do need to implement a custom request, this is all you need to do: Extend the Request<T> class, where <T>

represents the type of parsed response the request expects.

Implement the abstract methods parseNetworkResponse() and deliverResponse()

Page 41: Transmitting network data using volley(14 09-16)

parseNetworkResponse Method A Response encapsulates a parsed response for

delivery, for a given type (such as string, image, or JSON).

Page 42: Transmitting network data using volley(14 09-16)

deliverResponse() Method

Volley calls you back on the main thread with the object you returned in parseNetworkResponse().

Page 43: Transmitting network data using volley(14 09-16)

Example

Page 44: Transmitting network data using volley(14 09-16)

Example (Con)