Public code reference (volley)

Source: Internet
Author: User

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:

    • Define a cache class:
 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); 
    • Example
 Public classMainactivityextendsActivity {PrivateImageView Mimageview; PrivateNetworkimageview Mnetworkimageview; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mimageview=(ImageView) Findviewbyid (; Mnetworkimageview=(Networkimageview) Findviewbyid (;        Getjson ();        LoadImage ();        Networkimageview ();    Postjson (); }        //post jons data to server side    Private voidPostjson () {requestqueue requestqueue= Volley.newrequestqueue ( This); String Jsondataurl= "Http://"; 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://"; FinalProgressDialog ProgressDialog = ( 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://"; 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://"; 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)

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.