Volley download is mainly used to download text data and image data in two directions, the following are described separately;
First, the use of volley to open the download, firstly to do is to guide the package and add permissions;
(1) Import dependency package in Build.gradle file: Compile ' eu.the4thfloor.volley:com.android.volley:2015.05.28 ';
(2) in the manifest file to add access to network permissions, read, write external memory permissions;
Second, the text content upload download steps:
(1) Create Requestqueue object for sending request queue, and create Stringrequest object for package upload and download data string;
(2) Initialize data steps:
(2-1) initialize the request queue; Volley object calls Singleton mode Newrequestqueue (), passing in the context object;
(2-2) Initialize the request object of the Stringrequest class; pass in four parameters:
Parameter 1: Request method: Include Request.Method.GET or Request.Method.POST, default is download Request.Method.GET;
Parameter 2: Network request path;
Parameter 3: The instantiated request listener object; response.listener<t> in the class, rewrite Onresponse (String Response) to perform the operation after receiving the request result;
Parameter 4: an instantiated error listener; Errorlistener, overriding Onerorresponse (Volleyerror error) in the class to handle network errors;
(2-3) If the data is uploaded, after initializing the Stringrequest object, overriding the Getparams () in the class, and throwing the Authfailureerror error, the method returns the Map<string, string> type of data , the form of key-value pairs is uploaded to the network;
(2-4) In the action event, the Requestqueue object calls Add (), adding the Stringrequest object as a different request and uploading the packet for network operation;
(2-5) OnDestroy add Try-catch statement to catch all exceptions, to ensure that the interface normal exit, Requestqueue object calls Cancelall () incoming "get" and "post" two different forms of canceling the network;
Third, picture upload download steps:
(1) Create a Requestqueue object for sending request queue, and create Imageloader object and Imagerequest object to package the downloaded and uploaded images;
(2) There are three ways to download images:
(2-1) Image request method (similar to text request):
1) Initialize the picture Request object of the Imagerequest class and pass in six parameters:
Parameter 1: Picture download path;
The static listener of the parameter 2:response class listener<bitmap> the instantiated object, overriding the Onresponse () incoming Bitmap object as the resulting image;
Parameter 3: Picture width size, normal is 100;
Parameter 4: Picture height size, normal is 100;
Parameter 5: Picture chroma Way, Bitmap.Config.RGB_565;
The parameter 6:response class's static listener Errorlistener instantiates the object, overriding Onerrorresponse (), which calls this method when a picture error is requested;
2) Put the picture request object into the request queue; Requestqueue object calls Add (), passing in the Imagerequest object;
(4-2) Image loading mode:
1) Create a picture loading object for the Imageloader class, you can set the picture cache; Pass in two parameters:
Parameter 1: Request queue Requestqueue object;
Parameter 2: Implement a custom picture cache class that inherits Imageloader.imagecache;
2) custom Picture cache class inherits Imageloader.imagecache, defining a property of two methods:
Properties: Image Cache Size: First determine the size of the image cache, get the system running memory, Runtime.getruntime (). MaxMemory ()/1024/1024/8 get 1/8 of the current system running memory as the picture cache; Create LRUCache later <string, Bitmap> object, incoming picture cache size OK;
Override Getbitmap (String URL), call this method when getting a picture, return the bitmap object that the Lrucach object calls the Get () incoming URL;
Rewrite putbitmap (String url, Bitmap Bitmap), call this method when loading a picture, LRUCache object calls put () to the URL download path and Bitmap image;
3) Create a Imageloader.imagelistener singleton, Imagelistener call the static method Getimagelistener () and pass in three parameters:
Parameter 1: The control to be displayed for the picture;
Parameter 2: Display image resource ID before loading the picture;
Parameter 3: The image resource ID is displayed when the picture fails to load;
4) Loader object call get () Open download, incoming picture download path and Imagelistener object two parameters;
(4-3) using the network picture control mode Networkimageview:
1) initialize the Networkimageview control;
2) Create picture loading object of Imageloader class, can set picture cache;
3) Networkimageview object Call Setdefaultimageresid () sets the image resource ID before loading;
4) Networkimageview object Call Seterrorimageviewresid () sets the image resource ID when the load fails;
5) Networkimageview Object Call Setimageurl () pass in two parameters, set the loading picture;
Parameter 1: Picture download path;
Parameter 2:imageloader object;
Android Advanced _ Third party download tool volley