First on:
First write a volley of the requested class:
public void Fetchdata () {String URL = "http://2.novelread.sinaapp.com/framework-sae/index.php";//string body = "";//try {//mentity = new stringentity (body),//} catch (Unsupportedencodingexception E1) {//e1.printstacktrace ();//}//If an error occurs, Will enter this listener errorlistener Errorlistener = new Response.errorlistener () {@Overridepublic void Onerrorresponse (volleyerror arg0) {}};<span style= "White-space:pre" ></span>//if the requested server data, will enter this method//listener<string> Listener = new Response.listener<string> () {////@Override//public void Onresponse (String message) {//LOG.D (TAG, " message = "+message";//}//}; Requestqueue requestqueue = Volley.newrequestqueue (Mcontext); Stringrequest request = new Stringrequest (Request.Method.GET, URL, Mlistener, Errorlistener)//construct request{@Overrideprot ected response<string> parsenetworkresponse (networkresponse Response) {try {String jsonstring = NE W String (Response.data, "UTF-8"); Return Response.success (jsonstring, Httpheaderparser.parsecacheheaders (response)); } catch (Unsupportedencodingexception e) {return response.error (new ParseError (e)); } catch (Exception je) {return response.error (new ParseError (JE)); }} @Overridepublic map<string, string> getheaders () throws Authfailureerror {hashmap<string, string> header s = new hashmap<string, string> (); Headers.put ("Key", "value"); return headers;} @Overridepublic byte[] Getpostbody () throws Authfailureerror {Bytearrayoutputstream outputstream = new Bytearrayoutputstream (); try {Mentity.writeto (outputstream);} catch (IOException e) {log.e (TAG, "IOException @" + GetClass (). Getsimplename ());} return Outputstream.tobytearray ();} @Overridepublic String Getpostbodycontenttype () {return Mentity.getcontenttype (). GetValue ();}} overloaded method body; Requestqueue.add (request);}
In the activity, you just have to parse the data and put it in the ListView.
Mlistener = new Response.listener<string> () {@Overridepublic void Onresponse (String message) {LOG.D (TAG, "message = "+message");p Arsejson (message);}; Isshowlistview (FALSE); Getdataviavolley Valley = getdataviavolley.getinstance (this); Valley.setlistener (Mlistener); Valley.fetchdata ();} private void Parsejson (String content) {try {jsonarray json = new Jsonarray (content); String str = ""; for (int i = 0; I < json.length (); i++) {str = (String) json.getstring (i); Jsonarray Temparray = (jsonarray) json.get (i); Shopdata data = new Shopdata ();d ata.shopid = temparray.getstring (0);d ata.name = temparray.getstring (1);d Ata.url = Convert (temparray.getstring (2));d Ata.info = temparray.getstring (3); MAdapter.items.add (data);}} catch (Jsonexception e) {e.printstacktrace ();} Isshowlistview (True); madapter.notifydatasetchanged ();} private static string convert (String str) {return Str.replaceall ("\\\\", "");} private void Isshowlistview (Boolean flag) {mlistview.setvisibility (flag? View.VISIBLE:View.GONE); LoadinGlayout.setvisibility (flag?) View.GONE:View.VISIBLE);}
Picture loading is implemented using Networkimagview to see the layout of the ListView item:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Fill_parent" android:layout_height= "56DP" android:minheight= "56DP" > <com.android.volley.toolbox.networkimageview Android:id= "@+id/image_icon" android:layout_width= "@dimen/favorites_item_picture_width" Android:layout_heig ht= "@dimen/favorites_item_picture_height" android:layout_alignparentleft= "true" android:layout_marginleft= "5 DP "/> <relativelayout android:layout_width=" fill_parent "android:layout_height=" Fill_paren T "android:layout_marginleft=" @dimen/messages_list_view_text_margin_left "android:layout_torightof=" @+id/ima Ge_icon "android:background=" @drawable/bg_list_item_divider "> <textview android:id = "@+id/name" android:layout_width= "match_parent" android:layout_height= "Match_parent" and Roid:layout_alignparentleft= "true" android:layout_margintop= "10DP" android:ellipsize= "marquee" android:gravity= "cent Er_vertical|left "android:singleline=" true "android:textcolor=" #000000 "android:textsize= "@dimen/font_size_medium"/> <textview android:id= "@+id/info" Android:layout_wi Dth= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "15DP" a ndroid:layout_below= "@id/name" android:ellipsize= "marquee" android:gravity= "Center_vertical|left" Android:singleline= "true" android:textcolor= "#000000" android:textsize= "@dimen/font_size_sm All "/> </RelativeLayout></RelativeLayout>
The implementation of the adapter of the ListView:
private void Showimagebynetworkimageview (Networkimageview imageview,string url) {requestqueue requestqueue = V Olley.newrequestqueueindisk (This, "/sdcard/demo/", null); Imageloader Imageloader = new Imageloader (Requestqueue, New Bitmapcache ()); Imageview.settag ("url"); Imageview.setdefaultimageresid (R.drawable.big_head_icon); Imageview.setimageurl (Url,imageloader); } private class ListAdapter extends baseadapter{protected list<shopdata> items = new Arraylist<shopdata> ( );p ublic ListAdapter () {super ();} @Overridepublic int GetCount () {return items.size ();} @Overridepublic Object getItem (int position) {return items.get (position);} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Shopdatatag tag = new Shopdatatag ();//This purpose is to reuse convertviewif (Convertview = = null) {Convertview = minflater.inflate (R.layout.listitem, null); tag.name = (TextView) cOnvertview.findviewbyid (r.id.name); tag.shopinfo = (TextView) Convertview.findviewbyid (r.id.info); Tag.icon = ( Networkimageview) Convertview.findviewbyid (R.id.image_icon); Convertview.settag (tag);} Else{tag = (Shopdatatag) Convertview.gettag ();} TextView name = Tag.name; TextView info = tag.shopinfo; Networkimageview ImageView = Tag.icon; Shopdata data = items.get (position); Name.settext (data.name); Info.settext (Data.info); Showimagebynetworkimageview ( ImageView, Data.url); return convertview;}} Private class Shopdatatag{textview name; TextView Shopinfo; Networkimageview icon;}
Code can be downloaded in http://download.csdn.net/detail/baidu_nod/7606877