Today use a volley network communication framework, feel very good, write a blog record how to use, convenient later VC.
Volley (the network communication library provided by Google can make network communication faster, simpler and more robust.) )
Function module:
1. Asynchronous download of JSON, images, etc.
---------------------------------------------------------------------------------------
2. Ordering of network requests (scheduling)
---------------------------------------------------------------------------------------
3. Priority handling of network requests
---------------------------------------------------------------------------------------
4. Caching
---------------------------------------------------------------------------------------
5. Multi-level cancellation requests
---------------------------------------------------------------------------------------
6. Interaction with activity and life cycle (simultaneous cancellation of all network requests at end of activity)
Pros: Volley is especially suitable for scenarios where data is small but communication is frequent.
There are many csdn on the volley jar package and can be downloaded on GitHub.
Here is a simple example that I have written.
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 get JSON data * load images asynchronously with volley * Load network images with Networkimageview */ 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 (); } /** * using volley to get JSON data */ private void Getjsonbyvolley () { requestqueue RequestQueue = Volley.newrequestqueue (this); string JSONDataUrl = "url to access" ; final ProgressDialog progressdialog = ProgressDialog.show ( This, "The IS title", "... Loading ... "); jsonobjectrequest Jsonobjectrequest = new Jsonobjectrequest ( Request.Method.GET, jsondataurl, null, new Response.Listener< Jsonobject> () { @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); } /** * using volley to load images asynchronously * * Getimagelistener (ImageView view, int defaultimageresid, int errorimageresid) * First parameter: Displays the imageview * of the picture the second parameter: The picture resource that is displayed by default * the third parameter: Picture resource displayed when loading error &NBSP;&NBSP;&NBSP;&Nbsp; */ private void Loadimagebyvolley () { string imageurl= "url to access"; requestqueue Requestqueue = Volley.newrequestqueue (this); final LruCache <string, bitmap> lruCache = new lrucache<string, bitmap> (a); 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); } /** * using Networkimageview to display network pictures */ private void Showimagebynetworkimageview () { string Imageurl= "url to access"; requestqueue RequestQueue = Volley.newrequestqueue (this); final LruCache<String, bitmap> LruCache = new lrucache<string, bitmap> (; ) 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); } }
Layout file:
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:tools= "Http://schemas.android.com/tools" android:layout_width= "match_parent " android:layout_height=" > "Match_parent" <com.android.volley.toolbox.NetworkImageView android:id= "@+id/networkimageview" android:layout_width= "120dip" android:layout_height= "120dip" /> <imageview android : id= "@+id/imageview" android:layout_width= "120dip" android:layout_height= "120dip" /> </RelativeLayout>
The record is complete! The next time you use direct vc!
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Development Volley network communication framework