Android Volley framework (III), androidvolley

Source: Internet
Author: User

Android Volley framework (III), androidvolley

The Volley framework can be learned immediately. Haha, I am a little excited. The reason for this framework is divided into three articles, and there are many details involved, the reason is that we need to learn a few Android http frameworks later. As long as we read the volley Framework carefully, the learning of the following frameworks is easy ". Before starting this article
We recommend that you read the first two articles:

Use of Android Volley (1)

Use of Android Volley (2)

This article mainly introduces image request and cache, which seems to be the most commonly used in development. So let's end up with a Demo to practice it!

The article structure is as follows:

Image Request

For more convenient use of image requests in VolleyApplicationControllerClass to encapsulateImageLoader

Public class ApplicationController extends Application {/* do not repeat the methods previously written */private ImageLoader imageLoader ;.... public ImageLoader getImageLoader () {getRequestQueue (); // if imageLoader is empty, create it. The second parameter represents the class for processing image cache if (imageLoader = null) {imageLoader = new ImagerLoader (this. reqQueue, new LruBitmapCache ();} return this. imageLoader ;}} public class LruBitmapCache extends LruCache <String, Bitmap> implements ImageCache {public static int getDefaultLruCacheSize () {final int maxMemory = (int) (Runtime. getRuntime (). maxMemory/1024); final int cacheSize = maxMemory/8; return cacheSize;} public LruBitmapCache () {this (getDefaultLruBitmapCacheSize);} public LruBitmapCache (int sizeInKiloBytes) {super (sizeInkiloBytes) ;}@ Override public int sizeOf (String key, Bitmap Value) {return value. getRowBytes () * value. getHeight ()/1024 ;}@ Override public Bitmap getBitmap (String url) {return get (url) ;}@ Override public void putBitmap (String url, Bitmap bitmap) {put (url, bitmap );}}
 ImageLoader imageLoader=ApplicationController.getInstance().getImageLoader();

You can use an Image view provided by Volley,NetworkImageView, Just a few lines of code.

// Layout NetworkImageView in the layout file NetworkImageView imageView = (NetworkImageView) findViewById (R. id. networkimageview); // you need to use imageLoader imageView. setImageUrl (url, imageLoader );

To directly load an image into ImageView, use the following methods:

ImageLoader imageLoader = ApplicationController. getInstance (). getImageLoader (); imageLoader. get (url, new ImageListener () {@ Override public void onResponse (ImageContainer response, boolean arg) {if (response. getBitmap ()! = Null) {// set imageView. setImageBitmap (response. getBitmap () ;}}@ Override public void onErrorResponse (VolleyError error) {Log. e (TAG, "Image Error" + error. getMessage ());}});
Volley Cache

Volley has a powerful caching mechanism to maintain the requested cache, which saves unnecessary network consumption and waiting time. Below are some common cache methods.

Cache cache = ApplicationController. getRequestQueue (). getCache (); Entry entry = cache. get (url); if (Entry! = Null) {try {String data = new String (entry. data, "Utf-8"); // process data, convert it to JSON, XML, Bitmap, etc.} catch (UnspportedEncodingException e) {e. printStackTrace () ;}} else {// The cache does not exist for network requests}
ApplicationController.getInstance().getRequestQueue().getCache().invalidate(url,true);
StringRequest req=new StringRequest(...);req.setShouldCache(false);
ApplicationController.getInstance().getRequestQueue().getCache().remove(url);
ApplicationController.getInstance().getRequestQueue().getCache().clear(url);
Use Volley to retrieve data and populate custom ListViewFor space limitations, go to the next article to write->_< conclusion:

To sum up, I have learned how to use the Volley framework. I need to consider specific problems in practical application. If necessary, I need to learn to consult materials. Besides the reference materials mentioned in the above articles, it is best to go over the wall and check out google's official documentation on Volley. I want to solve our problems.

Reference: Android working with Volley Library

  • Weibo: @ mingsang Android Black History
  • Mailbox: <13141459344@163.com>
  • Personal Homepage: The history of mingsang's victory over Android Wang

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: info-contact@alibabacloud.com 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.