Network Communication tool volley

Source: Internet
Author: User
Request an image

Volley provides the classes to help developers request images on the server. These classes provide different levels for image processing.

  • Imagerequest: A Bitmap can be obtained through an image URL. This class provides convenient features such as changing the image size. The main benefit of schedule is to ensure the image encoding and resize through volley's thread schedule.

  • Imageloader: A large number of images can be obtained through URLs. For example, you need to put a large number of thumbnails in a listview. Imageloader provides a memory-level cache mechanism, which prevents image flickering. At the same time, imageloader also provides aggregate response, which can return multiple response at the same time, thus improving the performance.

  • Networkimageview: Built on imageloader, imageview can be completely replaced when obtaining remote images.

Use imagerequest
ImageRequest request = new ImageRequest(url,new Response.Listener() {    @Override    public void onResponse(Bitmap bitmap) {        mImageView.setImageBitmap(bitmap);    }}, 0, 0, null,new Response.ErrorListener() {    public void onErrorResponse(VolleyError error) {        mImageView.setImageResource(R.drawable.image_load_error);    }});

Remember to send a request to requestqueue using a single class.

Use imageloader and networkimageview

You can use the imageloader and network pairs to display multiple secondary images. This can be written in an XML file.

<com.android.volley.toolbox.NetworkImageView    android:id="@+id/networkImageView"    android:layout_width="150dp"    android:layout_height="170dp"    android:layout_centerHorizontal="true" />

You can also use only imageloader:

ImageLoader mImageLoader;ImageView mImageView; // The URL for the image that is being loaded.private static final String IMAGE_URL ="http://developer.android.com/images/training/system-ui.png";...mImageView = (ImageView) findViewById(R.id.regularImageView);// Get the ImageLoader through your singleton class.mImageLoader = MySingleton.getInstance(this).getImageLoader();mImageLoader.get(IMAGE_URL, ImageLoader.getImageListener(mImageView,R.drawable.def_image, R.drawable.err_image));

Of course, they can also be used together.

Why is the single-class mode used. Refer:

This approach ensures that your app creates single instances of these classes that last the lifetime of your app. the reason that this is important for imageloader (the helper class that handles loading and caching images) is that the main function of the in-memory cache is to allow for flickerless rotation. using a Singleton pattern allows the bitmap cache to outlive the activity. if instead you create the imageloader in an activity, the imageloader wocould be recreated along with the activity every time the user rotates the device. this wocould cause flickering.

An example of LRU Cache

Volley provides a standard cache implementation through the diskbasedcache class. This class is willing to directly cache images to a specific directory in the disk file. But when you use imageloader, you should implement the imageloader. imagecache interface.

public class LruBitmapCache extends LruCache<String, Bitmap>    implements ImageCache {public LruBitmapCache(int maxSize) {    super(maxSize);}public LruBitmapCache(Context ctx) {    this(getCacheSize(ctx));}@Overrideprotected int sizeOf(String key, Bitmap value) {    return value.getRowBytes() * value.getHeight();}@Overridepublic Bitmap getBitmap(String url) {    return get(url);}@Overridepublic void putBitmap(String url, Bitmap bitmap) {    put(url, bitmap);}// Returns a cache size equal to approximately three screens worth of images.public static int getCacheSize(Context ctx) {    final DisplayMetrics displayMetrics = ctx.getResources().            getDisplayMetrics();    final int screenWidth = displayMetrics.widthPixels;    final int screenHeight = displayMetrics.heightPixels;    // 4 bytes per pixel    final int screenBytes = screenWidth * screenHeight * 4;    return screenBytes * 3;}}

This is the cache that imageloader uses.

RequestQueue mRequestQueue; // assume this exists.ImageLoader mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache(        LruBitmapCache.getCacheSize()));
Request a JSON

Class description:

  • Jsonarrayrequest: A jsonarray is returned Based on the URL.
  • Jsonobjectrequest: A jsonobject can be returned, and a jsonobject can be passed as the request body (through gson)

Network Communication tool 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: 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.