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.
Usage
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 = It.next (); 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 = Objit.next (); 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