Android new Internet mode volley use (a) loading images with JSON

Source: Internet
Author: User

Recently just contact with Android, used to make WP, a pair compared to the Android how so troublesome. Networking must be reopened to a thread. and loading network pictures is also very troublesome ... It took me a long time to get stuck on the listview of a quick slide load network picture, and it's always been a pain ...

But today found a new way of networking, grateful to Google Ah. Volley

Suddenly found before those httpclient or httpurlconnection all go to die. Or you use AH.


ImageVIew Loading Pictures

Core Imagerequest

1 First

private Requestqueue Mqueue;      

2 Initialize it in the OnCreate method

Mqueue = Volley.newrequestqueue (this);

3 Create a imagerequest object and add it to the mqueue

public void Readbitmapviavolley (String imgurl, final ImageView ImageView) {imagerequest imgrequest = new Imagerequest (Imgurl,new response.listener<bitmap> () {@Override publicvoid Onresponse (Bitmap arg0) {//TODO Auto-generated method Stubimageview.setimagebitmap (arg0);},   //Purple code means  new Errorlistener () {@ overridepublic void Onerrorresponse (Volleyerror arg0) {}});  Callback after a failed networking.  Mqueue.add (imgrequest);//Add to Mqueue, complete the binding.

In the above, we can see that in the Imagerequest constructor, we can directly transfer some parameters about the bitmap, such as the length and width of the information, The result returned in the Response.listener returned is a bitmap, and the ImageView is set in the Onresponse function and added to the Mqueue finally.

The above code and httpclient are the same as httpurlconnection in multi-threading, but the code is a lot more concise. Haha once again thanks to Google.

ImageVIew Loading JSON

First, old.

Private Requestqueue Mqueue;

Mqueue = Volley.newrequestqueue (this);

public void Getweatherinfo () {jsonobjectrequest jsonobjectrequest = new Jsonobjectrequest (Weather_link, NULL, new Response.listener<jsonobject> () {@Overridepublic void Onresponse (Jsonobject arg0) {list.clear ();iterator< String> it = Arg0.keys (); while (It.hasnext ()) {String key = (); Jsonobject obj = null;try {obj = Arg0.getjsonobject (key);} catch (Jsonexception e) {//TODO auto-generated catch BLOCKE.P Rintstacktrace ();} if (obj! = null) {iterator<string> Objit = Obj.keys (); while (Objit.hasnext ()) {String Objkey = (); String objvalue;try {objvalue = obj.getstring (Objkey); hashmap<string, string> map = new hashmap<string, string> (); Map.put ("title", Objkey); Map.put ("Content", ObjValue); LOG.V (TAG, "title =" + Objkey + "| Content = "+ ObjValue); List.add (map);} catch (Jsonexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} LOG.V (TAG, "list.size =" + List.size ())}, new Response.errorlistener () {@Overridepublic void OnerrorrespoNSE (Volleyerror arg0) {}}); Mqueue.add (jsonobjectrequest); 


Depending on the JSON data, you can use Jsonarrayrequest or jsonobjectrequest

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.