1. First download the volley Jar package and import it into your own project
Volley jar Package Download
2.JsonArrayRequest Simple Text Request
Use the following code to implement JSON data acquisition
Requestqueue queue = Volley.newrequestqueue (instance);//Note here The Requestqueue is a request queue object that can cache all HTTP requests and then make these requests in parallel with a certain algorithm. Requestqueue internal design is very suitable for high concurrency, so we do not have to create a Requestqueue object for every HTTP request, which is a waste of resources, Basically creating a Requestqueue object in every activity that needs to interact with the network is sufficient. //TODO auto-generated method stubString URL ="http://112.74.78.53/scut-lib/library/hotReading.php"; Jsonarrayrequest jsobjrequest =NewJsonarrayrequest (URL,NewResponse.listener<jsonarray> () {@Override Public void Onresponse(Jsonarray response) {//TODO auto-generated method stubJson_text.settext ("Response ="+ response.tostring ()); } },NewResponse.errorlistener () {@Override Public void Onerrorresponse(Volleyerror error) {//TODO auto-generated method stubLOG.E ("Error", Error.getmessage ()); } }); Queue.add (jsobjrequest);
Also, since volley is going to access the network, don't forget to add the following permissions in your Androidmanifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
However, there are usually two types of requests for HTTP, get and post, and what we are using is obviously a GET request, so what do you do if you want to issue a POST request? Since Jsonarrayrequest does not provide a POST request, let's look at Stringrequest's POST request:
Stringrequest Stringrequest= NewStringrequest (Method.POST, URL, Listener, Errorlistener) {@Overrideprotected Map<String,String>Getparams () throws Authfailureerror {Map<String,String> Map = NewHashMap<String,String>();Map.Put"Params1","Value1");Map.Put"PARAMS2","value2");return Map; } };
3. Use volley to load Network Pictures 3.1 use imagerequest to load network pictures
Queue = Volley.newrequestqueue ( This); Imagerequest imagerequest =NewImagerequest ("Http://pic.nipic.com/2007-11-09/2007119124513598_2.jpg",NewResponse.listener<bitmap> () {@Override Public void Onresponse(Bitmap arg0) {//TODO auto-generated method stubJson_img.setimagebitmap (arg0); } },0,0, config.rgb_565,NewResponse.errorlistener () {@Override Public void Onerrorresponse(Volleyerror arg0) {//TODO auto-generated method stubJson_img.setimageresource (R.drawable.ic_launcher); } }); Queue.add (imagerequest);
Imagerequest's constructor receives six parameters, the first parameter is the URL address of the picture, and there is nothing to explain. The second parameter is a successful callback for the picture request, where we set the returned bitmap parameter to ImageView. The third and fourth parameters are used to specify the maximum allowable width and height of the picture, if the specified network picture width or height is greater than the maximum value here, the picture will be compressed, specified as 0, no matter how large the picture, will not be compressed. The fifth parameter is used to specify the color properties of the picture, and several constants under Bitmap.config can be used here, where argb_8888 can show the best color properties, each pixel occupies a size of 4 bytes, and rgb_565 indicates that each picture pixel occupies 2 bytes. The sixth parameter is a callback for the failed picture request, where we display a default picture in ImageView when the request fails.
3.2 Using Imageloader to load network pictures
Imageloader can also be used to load pictures on the network, and its interior is also implemented using Imagerequest, but imageloader is significantly more efficient than imagerequest because it not only helps us to cache images, You can also filter out duplicate links to avoid sending requests repeatedly.
Since Imageloader is not inherited from request, its usage is different from what we have learned before, which can be summed up roughly in the following four steps:
-
Creates a Requestqueue object.
-
Creates a Imageloader object.
-
Gets a Imagelistener object.
-
Call Imageloader's Get () method to load a picture on the network.
//imageloader's constructor receives two parameters, the first parameter is the Requestqueue object, the second argument is a Imagecache object, and here we first new an empty Imagecache implementationImageloader Imageloader =NewImageloader (Queue,NewImagecache () {@Override Public void Putbitmap(String URL, Bitmap Bitmap) { }@Override PublicBitmapGetbitmap(String URL) {return NULL; } });Next we need to get a Imagelistener object, we can get to a Imagelistener object by calling Imageloader's Getimagelistener () method, Getimagelistener () The ImageView method receives three parameters, the first parameter specifies the control that is used to display the picture, the second parameter specifies the picture that is displayed during the loading of the picture, and the third parameter specifies the picture to display if the picture fails to loadImagelistener listener = Imageloader.getimagelistener (json_img, R.drawable.ic_launcher, r.drawable.ic_l Auncher);//Call Imageloader's Get () method to load the picture, or if you want to limit the size of the picture, you can also use the Get () method overload to specify the maximum allowable width and height of the picture//imageloader.get ("http://pic.nipic.com/2007-11-09/2007119124513598_2.jpg", listener, 200, 200); Imageloader.get ("Http://pic.nipic.com/2007-11-09/2007119124513598_2.jpg", listener);
Although now we have mastered the use of imageloader, but the advantages of the Imageloader just introduced have not been used. Why is it? Because the Imagecache object created here is an empty implementation, it does not function as a picture cache at all. Writing a imagecache is also very simple, but if you want to write a very good imagecache, it is best to use the LRUCache features provided by Android.
LRUCache features can be consulted:
http://blog.csdn.net/guolin_blog/article/details/9316683
Create a new Bitmapcache and implement the Imagecache interface
Public class Bitmapcache implements Imagecache { PrivateLrucache<string, bitmap> Mcache; Public Bitmapcache() {intMaxSize =Ten*1024x768*1024x768; Mcache =NewLrucache<string, bitmap> (maxSize) {@Override protected int sizeOf(String key, Bitmap Bitmap) {returnBitmap.getrowbytes () * Bitmap.getheight (); } }; }@Override PublicBitmapGetbitmap(String URL) {returnMcache.get (URL); }@Override Public void Putbitmap(String URL, Bitmap Bitmap) {mcache.put (URL, bitmap); } }
newnew
3.3 Using Networkimageview to load network pictures
Networkimageview is a custom control that inherits from the ImageView, has all the functions of the ImageView control, and adds the ability to load the network picture on top of the native base. Networkimageview controls are easier to use than the first two, and can be broadly divided into the following five steps:
-
Creates a Requestqueue object.
-
Creates a Imageloader object.
-
Add a Networkimageview control to the layout file.
-
Gets an instance of the control in code.
-
Sets the image address to load.
Imageloader Imageloader =NewImageloader (Queue,NewImagecache () {@Override Public void Putbitmap(String URL, Bitmap Bitmap) { }@Override PublicBitmapGetbitmap(String URL) {return NULL; } }); Img_view.setdefaultimageresid (R.drawable.ic_launcher); Img_view.seterrorimageresid (R.drawable.ic_launcher); Img_view.setimageurl ("Http://pic.nipic.com/2007-11-09/2007119124513598_2.jpg", Imageloader);
Reference http://blog.csdn.net/guolin_blog/article/details/17482095
Android Network Framework Volley