Volley network communication framework developed for Android

Source: Internet
Author: User

Volley network communication framework developed for Android

Today, I used the Volley network communication framework, which is quite helpful. I wrote a blog to record how to use it, so that later I could use VC.

 

Volley (the network communication library provided by Google enables faster, simpler, and more robust network communication .)

Function module:


1. asynchronous download of JSON and Images
Bytes ---------------------------------------------------------------------------------------
2. Sort network requests (scheduling)
Bytes ---------------------------------------------------------------------------------------
3. priority processing of network requests
Bytes ---------------------------------------------------------------------------------------
4. Cache
Bytes ---------------------------------------------------------------------------------------
5. Multi-Level cancellation request
Bytes ---------------------------------------------------------------------------------------
6. Interaction with Activity and lifecycle (canceling all network requests at the end of Activity)




Advantage: Volley is especially suitable for scenarios with small data volumes but frequent communication.

 

Many Volley jar packages are available on CSDN. You can also download them on GitHub.

 

The following is a simple example.

 

 

Import org. json. JSONObject; import android. app. activity; import android. app. progressDialog; import android. graphics. bitmap; import android. OS. bundle; import android. support. v4.util. lruCache; import android. widget. imageView; import com. android. volley. request; import com. android. volley. requestQueue; import com. android. volley. response; import com. android. volley. volleyError; import com. android. volley. toolbox. imageLoader; import com. android. volley. toolbox. imageLoader. imageCache; import com. android. volley. toolbox. imageLoader. imageListener; import com. android. volley. toolbox. jsonObjectRequest; import com. android. volley. toolbox. networkImageView; import com. android. volley. toolbox. volley;/*** Demo Description: * use Volley to obtain JSON data * use Volley to asynchronously load images * use NetworkImageView to load network images */public class MainActivity extends Activity {private ImageView mImageView; private NetworkImageView mNetworkImageView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init ();} private void init () {mImageView = (ImageView) findViewById (R. id. imageView); mNetworkImageView = (NetworkImageView) findViewById (R. id. networkImageView); getJSONByVolley (); loadImageByVolley (); showImageByNetworkImageView ();}/*** use Volley to obtain JSON data */private void getJSONByVolley () {RequestQueue requestQueue = Volley. newRequestQueue (this); String JSONDataUrl = URL to be accessed; final ProgressDialog ssssdialog = progressDialog. show (this, This is title ,... loading ...); jsonObjectRequest jsonObjectRequest = new JsonObjectRequest (Request. method. GET, JSONDataUrl, null, new Response. listener
 
  
() {@ Override public void onResponse (JSONObject response) {System. out. println (response = + response); if (progressDialog. isShowing () & progressDialog! = Null) {progressDialog. dismiss () ;}}, new Response. errorListener () {@ Override public void onErrorResponse (VolleyError arg0) {System. out. println (sorry, Error) ;}}); requestQueue. add (jsonObjectRequest);}/*** use Volley to asynchronously load the image ** getImageListener (ImageView view, int defaultImageResId, int errorImageResId) * The first parameter: imageView * second parameter: default image resource displayed * third parameter: image resource displayed when an error is loaded */private void loadImageByVolley () {String imageUrl = URL to be accessed; RequestQueue requestQueue = Volley. newRequestQueue (this); final LruCache
  
   
LruCache = new LruCache
   
    
(20); ImageCache imageCache = new ImageCache () {@ Override public void putBitmap (String key, Bitmap value) {lruCache. put (key, value) ;}@ Override public Bitmap getBitmap (String key) {return lruCache. get (key) ;}}; ImageLoader imageLoader = new ImageLoader (requestQueue, imageCache); ImageListener listener = ImageLoader. getImageListener (mImageView, R. drawable. ic_launcher, R. drawable. ic_launcher); imageLoader. get (imageUrl, listener);}/*** use NetworkImageView to display network images */private void showImageByNetworkImageView () {String imageUrl = URL to be accessed; RequestQueue requestQueue = Volley. newRequestQueue (this); final LruCache
    
     
LruCache = new LruCache
     
      
(20); ImageCache imageCache = new ImageCache () {@ Override public void putBitmap (String key, Bitmap value) {lruCache. put (key, value) ;}@ Override public Bitmap getBitmap (String key) {return lruCache. get (key) ;}}; ImageLoader imageLoader = new ImageLoader (requestQueue, imageCache); mNetworkImageView. setImageUrl (imageUrl, imageLoader );}}
     
    
   
  
 


 

 

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.