Android premi_third-Party Download tool Volley and androidvolley

Source: Internet
Author: User

Android premi_third-Party Download tool Volley and androidvolley

Volley downloads are mainly used to download text data and image data;

1. Use Volley to enable download. The first thing to do is to export the package and add permissions;

(1) import the dependency package: compile 'EU. the4thfloor. volley: com. android. volley: 2015.05.28 'in the build. gradle file ';

(2) Add the network access permission, read and write permissions to the list file;

Ii. Steps for uploading and downloading text content:

(1) create a RequestQueue object for sending the Request queue; create a StringRequest object for packaging the data strings for upload and download;

(2) Data initialization steps:

(2-1) initialize the Request queue. the Volley object calls the singleton mode newRequestQueue () and passes in the context object;

(2-2) initialize the request object of the StringRequest class, and pass in four parameters:

Parameter 1: Request Method: including Request. Method. GET or Request. Method. POST. The default value is to download Request. Method. GET;

Parameter 2: Network Request Path;

Parameter 3: instantiated request listening object; Response. Listener <T>, in which onResponse (String response) is rewritten to perform the operation after receiving the request result;

Parameter 4: instantiated error listening object; ErrorListener, in which onErorResponse (VolleyError error) is rewritten to handle network errors;

(2-3) if it is to upload data, after initializing the StringRequest object, rewrite getParams () in this class and throw the AuthFailureError. This method returns Map <String, string> type of data, transmitted to the network as a key-value pair;

(2-4) in an operation event, the RequestQueue object calls add () and adds the StringRequest object as different requests and upload data packets for network operations;

(2-5) in onDestroy, add the try-catch statement to catch all exceptions to ensure that the interface Exits normally. The RequestQueue object calls cancelAll () transfer "get" and "post" to cancel the network;

3. Steps for uploading and downloading images:

(1) create a RequestQueue object for sending a request queue; create an ImageLoader object and an ImageRequest object for packaging and downloading and uploading images;

(2) There are three ways to download images:

(2-1) image Request Method (similar to text request ):

1) initialize the image request object of the ImageRequest class and input six parameters:

Parameter 1: Image download path;

Parameter 2: static Listener of the Response class <Bitmap> instantiate the object, and override onResponse () to pass in the Bitmap object as the obtained image;

Parameter 3: The image width, which is 100;

Parameter 4: The image height, which is 100;

Parameter 5: Bitmap. Config. RGB_565;

Parameter 6: static listener of the Response class ErrorListener instantiates the object and overwrites onErrorResponse (). This method is called when the requested image is incorrect;

2) Put the image request object into the request queue; The RequestQueue object calls add () and passes in the ImageRequest object;

(4-2) image loading method:

1) create an image loading object of the ImageLoader class. You can set the image cache and input two parameters:

Parameter 1: Request queue RequestQueue object;

Parameter 2: Implement custom image cache class inheriting ImageLoader. ImageCache;

2) The custom image cache class inherits from ImageLoader. ImageCache and defines an attribute in two methods:

Attribute: Image cache size: first, determine the image cache size and obtain the system running memory, Runtime. getRuntime (). maxMemory ()/1024/1024/8 obtains 1/8 of the memory running on the current system as the image cache; then creates an LruCache <String, Bitmap> object and determines the image cache size;

Override getBitmap (String url) and call this method when obtaining the image. The LruCach object is returned and the Bitmap object passed in the url is called get;

Override putBitmap (String url, Bitmap bitmap) and call this method when loading images. The LruCache object calls put () to input the url download path and bitmap image;

3) create an ImageLoader. ImageListener singleton. ImageListener calls the static method getImageListener () and passes in three parameters:

Parameter 1: controls to be displayed;

Parameter 2: Id of the displayed image resource before loading the image;

Parameter 3: The image resource Id is displayed after the image fails to be loaded;

4) The Loader object calls get () to enable download and transmits two parameters: the image download path and the ImageListener object;

(4-3) NetworkImageView using the network image control:

1) initialize the NetworkImageView control;

2) create an image loading object in the ImageLoader class and set the image cache;
3) The NetworkImageView object calls setDefaultImageResId () to set the image resource Id before loading;

4) The NetworkImageView object calls setErrorImageViewResId () to set the image resource Id when loading fails;

5) The NetworkImageView object calls setImageUrl () to input two parameters and set to load the image;

Parameter 1: Image download path;

Parameter 2: ImageLoader object;


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