Volley usage (official website) and volley usage Official Website

Source: Internet
Author: User

Volley usage (official website) and volley usage Official Website

Download the library file from the official website. If you cannot log on, you can download the integrated. jar from here. Use it as the support library of your project after the download.

Send a simple request:
Final TextView mTextView = (TextView) findViewById (R. id. text );... // instantiate Request queue RequestQueue queue = Volley. newRequestQueue (this); String url = "http://www.google.com"; // request String from the supplied URL. StringRequest stringRequest = new StringRequest (Request. method. GET, url, new Response. listener <String> () {@ Override public void onResponse (String response) {// Display the first 500 characters of the response string. mTextView. setText ("Response is:" + response. substring (0,500) ;}}, new Response. errorListener () {@ Override public void onErrorResponse (VolleyError error) {mTextView. setText ("That d Idn' t work! ") ;}}); // Add this request to the Request queue. Queue. add (stringRequest );
Create a request queue:

Create a queue Singleton

/*** Created by peng on 2015/6/18. */public class MySingleton {private static MySingleton mInstance; private RequestQueue mRequestQueue; private ImageLoader mImageLoader; private static Context mCtx; private MySingleton (Context context) {mCtx = context; mRequestQueue = getRequestQueue (); mImageLoader = new ImageLoader (mRequestQueue, new ImageLoader. imageCache () {private final LruCache <String, Bitmap> cache = new LruCache <String, Bitmap> (20); @ Override public Bitmap getBitmap (String url) {return cache. get (url) ;}@ Override public void putBitmap (String url, Bitmap bitmap) {cache. put (url, bitmap) ;}}) ;}public static synchronized MySingleton getInstance (Context context) {if (mInstance = null) {mInstance = new MySingleton (context );} return mInstance;} public RequestQueue getRequestQueue () {if (mRequestQueue = null) {// getApplicationContext () This method is critical to mRequestQueue = Volley. newRequestQueue (mCtx. getApplicationContext ();} return mRequestQueue;} public <T> void addToRequestQueue (Request <T> req) {getRequestQueue (). add (req);} public ImageLoader getImageLoader () {return mImageLoader ;}}

Initiate a request with a queue Singleton

// Obtain the Request queue RequestQueue queue = MySingleton. getInstance (this. getApplicationContext ()). getRequestQueue ();... // Add a request to the queue MySingleton. getInstance (this ). addToRequestQueue (stringRequest );
Request an image:

ImageRequest: it can ensure that image requests occur in the working thread.
ImageLoader: it provides a memory cache and uses it to set an image to improve performance.
NetworkImageView: it is based on ImageLoader. It is more effective than ImageView when you request images from the network.

Use ImageRequest
ImageView mImageView; String url = "http:// I .imgur.com/7spzG.png"; mImageView = (ImageView) findViewById (R. id. myImage );... // retrieve the image from the specified URL and display it on the UI. ImageRequest request = new ImageRequest (url, new Response. listener <Bitmap> () {@ 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) ;}}); // use a single instance to access the Request queue MySingleton. getInstance (this ). addToRequestQueue (request );
Use ImageLoader
ImageLoader mImageLoader; ImageView mImageView; private static final String IMAGE_URL = "http://developer.android.com/images/training/system-ui.png ";... mImageView = (ImageView) findViewById (R. id. regularImageView); // obtain ImageLoader from the singleton class. MImageLoader = MySingleton. getInstance (this). getImageLoader (); mImageLoader. get (IMAGE_URL, ImageLoader. Metadata (mImageView, R. drawable. def_image, R. drawable. err_image ));
Use ImageLoader and NetworkImageView
<com.android.volley.toolbox.NetworkImageView        android:id="@+id/networkImageView"        android:layout_width="150dp"        android:layout_height="170dp"        android:layout_centerHorizontal="true" />
ImageLoader mImageLoader; NetworkImageView mNetworkImageView; private static final String IMAGE_URL = "http://developer.android.com/images/training/system-ui.png ";... mNetworkImageView = (NetworkImageView) findViewById (R. id. networkImageView); // obtain ImageLoader from the singleton class. MImageLoader = MySingleton. getInstance (this). getImageLoader (); // set the URL of the image to be loaded and the ImageLoader mNetworkImageView. setImageUrl (IMAGE_URL, mImageLoader) used to execute the request );

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.