Volley is a network library provided by Google, compared to their own writing httpclient is really convenient, this article reference part of the online example collation as follows, in order to make a memo:
Public classBitmapcacheImplementsImagecache {PrivateLrucache<string, bitmap>Cache; PublicBitmapcache () {/*LRUCache uses a linkedhashmap to implement a simple memory cache, without soft references, which are strongly referenced. If the added data is greater than the set maximum, the first cached data is deleted to adjust memory. */Cache=NewLrucache<string, bitmap> (8 * 1024 * 1024) {@Overrideprotected intsizeOf (String key, Bitmap Bitmap) {returnBitmap.getrowbytes () *bitmap.getheight (); } }; } @Override PublicBitmap getbitmap (String url) {returncache.get (URL); } @Override Public voidputbitmap (String URL, Bitmap Bitmap) {cache.put (URL, Bitmap);
Public classMainactivityextendsActivity {PrivateImageView Mimageview; PrivateNetworkimageview Mnetworkimageview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mimageview=(ImageView) Findviewbyid (R.id.imageview); Mnetworkimageview=(Networkimageview) Findviewbyid (R.id.networkimageview); Getjson (); LoadImage (); Networkimageview (); Postjson (); } //post jons data to server side Private voidPostjson () {requestqueue requestqueue= Volley.newrequestqueue ( This); String Jsondataurl= "Http://192.168.0.28:3000/postjson"; Map<string, string> map =NewHashmap<string, string>(); Map.put ("Key1", "value1"); Map.put ("Key2", "value2"); Jsonobject Jsonobject=Newjsonobject (map); Jsonobjectrequest jsonobjectrequest=Newjsonobjectrequest (Request.Method.POST, Jsondataurl, Jsonobject,NewResponse.listener<jsonobject>() {@Override Public voidOnresponse (jsonobject response) {System.out.println ("Response=" +response); } }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (Volleyerror arg0) {System.out.println ("Sorry,error" +arg0.getmessage ()); } }); Requestqueue.add (jsonobjectrequest); } //get JSON data from server Private voidGetjson () {requestqueue requestqueue= Volley.newrequestqueue ( This); String Jsondataurl= "Http://192.168.0.28:3000/"; FinalProgressDialog ProgressDialog = Progressdialog.show ( This, "This is title", "... Loading ... "); Jsonobjectrequest jsonobjectrequest=Newjsonobjectrequest (Request.Method.GET, Jsondataurl,NULL, NewResponse.listener<jsonobject>() {@Override Public voidOnresponse (jsonobject response) {System.out.println ("Response=" +response); if(progressdialog.isshowing ()&& ProgressDialog! =NULL) {Progressdialog.dismiss (); } } }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (Volleyerror arg0) {System.out.println ("Sorry,error" +arg0.getmessage ()); } }); Requestqueue.add (jsonobjectrequest); } //loading picture resources with Imageloader Private voidLoadImage () {String imageUrl= "Http://192.168.0.28:3000/getimage"; Requestqueue Requestqueue= Volley.newrequestqueue ( This); Imageloader Imageloader=NewImageloader (Requestqueue,NewBitmapcache ()); Imagelistener Listener=Imageloader.getimagelistener (Mimageview, R.drawable.test, r.drawable.test); Imageloader.get (IMAGEURL, Listener,200, 200); } //loading picture resources with Networkimageview components Private voidNetworkimageview () {String imageUrl= "Http://192.168.0.28:3000/getimage"; Requestqueue Requestqueue= Volley.newrequestqueue ( This); Imageloader Imageloader=NewImageloader (Requestqueue,NewBitmapcache ()); Mnetworkimageview.settag ("url"); Mnetworkimageview.setimageurl (IMAGEURL, Imageloader); }}
Problems encountered:
1. "Error Generating final archive:found duplicate file for Apk:androidmanifes", there is a problem with the referenced jar package, It contains res and manifest.xml, re-downloaded on the Internet one on the OK;
2, Imageloader.get () report null pointer error, in fact, Findviewbyid wrote to Setcontentview front, resulting in a resource handle is not taken; This low-level error, Eclipse Engineering is not to be reminded. , the program error to ImageView in get to the network picture before loading local default image is only error, toss a half-day Ah!!
Public code reference (volley)