Android uses volley to asynchronously load data (JSON and pictures) for a complete example

Source: Internet
Author: User

Android uses volley to asynchronously load data (JSON and pictures) for a complete example

Mainactivity.java

Package Cc.testvolley; 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: * 1 using volley to get JSON data * 2 loading picture asynchronously with volley * 1 loading the network image with Networkimageview * Reference: * 1 http://blog.csdn.net/t1 2x3456/article/details/9221611 * 2 http://blog.csdn.net/xyz_lmn/article/details/12063561 * 3 http://blog.csdn.net/ lfdfhl/article/details/12221875 * Thank you veRY Much */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 (); }/** * Get JSON data using volley */private void Getjsonbyvolley () {Requestqueue requestqueue = Volley         . Newrequestqueue (this);         String Jsondataurl = "Http://pipes.yahooapis.com/pipes/pipe.run?_id=giWz8Vc33BG6rQEQo_NLYQ&_render=json"; 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 OnR                         Esponse (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); }/** * Load picture asynchronously with volley * * Note method parameters: * Getimagelistener (ImageView view, int Defaultimager   Esid, int errorimageresid)  * First parameter: Show Picture's ImageView * Second parameter: Picture resource displayed by default * Third parameter: Picture resource displayed when loading error */private void Loadimagebyvolley () {         String imageurl= "Http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";         Requestqueue Requestqueue = Volley.newrequestqueue (this);         Final lrucache<string, bitmap> LruCache = new lrucache<string, bitmap> (20); Imagecache Imagecache = new Imagecache () {@Override public void Putbitmap (String key, Bitmap Valu             e) {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 pictures */private void Showimagebynetworkimageview () {String imageurl= "http://avatar.csdn.net/6/6/D/1_lfdfhl.jpg";         Requestqueue Requestqueue = Volley.newrequestqueue (this);         Final lrucache<string, bitmap> LruCache = new lrucache<string, bitmap> (20); Imagecache Imagecache = new Imagecache () {@Override public void Putbitmap (String key, Bitmap Valu             e) {lrucache.put (key, value);             } @Override Public Bitmap getbitmap (String key) {return lrucache.get (key);         }         };         Imageloader Imageloader = new Imageloader (Requestqueue, Imagecache);         Mnetworkimageview.settag ("url");     Mnetworkimageview.setimageurl (Imageurl,imageloader); }    }

Main.xml:

<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"         android:layout_centerhorizontal= "true"         android:layout_margintop= "30dip"     />             <imageview         android:id= "@+id/imageview"         Android:layout_width= "120dip"         android:layout_height= "120dip"         android:layout_centerinparent= "true"     />    </RelativeLayout>

  

Android uses volley to asynchronously load data (JSON and pictures) for a complete example

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.