The premise is to refer to volley's asynchronous mechanism and introduce its jar package
Public voidGetjsonbyvolley () {//mcontext is context, Requestqueue Requestqueue=Volley.newrequestqueue (Mcontext); String Jsondataurl= "http://sjshop.easyder.com/app/order_index/getCart?buyer_id=511"; FinalProgressDialog ProgressDialog = progressdialog.show (Mcontext, "Downloading", "Loading ..."); Progressdialog.setcancelable (true);//ProgressDialog can cancelJsonobjectrequest jsonobjectrequest =Newjsonobjectrequest (Request.Method.GET, Jsondataurl,NULL, NewResponse.listener<jsonobject>() {@Override Public voidOnresponse (jsonobject response) {Try{Getcartjson (response); } Catch(Exception e) {e.getmessage (); } if(Progressdialog.isshowing () && progressdialog! =NULL) {Progressdialog.dismiss (); } } }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (Volleyerror arg0) {System.out.println ("Sorry,error"); } }); Requestqueue.add (jsonobjectrequest); LOG.I (TAG,"Shopbeanlist----> Parsing JSON"); } //Getcart Network Parsing, parameter-->jsonobject Public voidGetcartjson (Jsonobject jsonobject) {//Create a store listList<shopbean> shopbeanlist=NewArraylist<shopbean>(); Jsonobject Objectinfo=NULL; Try{objectinfo= Jsonobject.getjsonobject ("info"); Jsonarray Arraygroup= Objectinfo.getjsonarray ("group"); for(inti = 0; I <arraygroup.length (); i++) {Jsonobject Item=Arraygroup.getjsonobject (i); Shopbean Shopbean=NewShopbean (item); Shopbeanlist.add (Shopbean); LOG.D (TAG,"Shopbeanlist----->" +item); } } Catch(jsonexception e) {e.printstacktrace (); } if(mcallback!=NULL) {mcallback.getdat (shopbeanlist); } }
An Jsonobject constructor was added to the entity class
Shopbean Entity classes:
/*** Created by Administrator on 2015/7/19 0019.*/ Public classShopbean {Private intSeller_uid;//Store ID PrivateString Seller_name;//Shop Name PublicList<goodsbean> goodslist;//Product Information List PublicString Getseller_name () {returnSeller_name; } Public voidsetseller_name (String seller_name) { This. Seller_name =Seller_name; } Public intGetseller_uid () {returnSeller_uid; } PublicList<goodsbean>getgoodslist () {returngoodslist; } Public voidSetgoodslist (list<goodsbean>goodslist) { This. goodslist =goodslist; } Public voidSetseller_uid (intseller_uid) { This. Seller_uid =Seller_uid; } PublicShopbean () {}//constructor, parameter is Jsonobject type PublicShopbean (Jsonobject jsonobject) { This. Seller_uid = Jsonobject.optint ("Seller_uid"); This. Seller_name = jsonobject.optstring ("Seller_name"); Goodsbean Goodsbean; Jsonarray Jsonarray= Jsonobject.optjsonarray ("goods"); if(Jsonarray! =NULL&& jsonarray.length () > 0) { Try{goodslist=NewArraylist<>(); for(intj = 0; J < Jsonarray.length (); J + +) {Goodsbean=NewGoodsbean (Jsonarray.getjsonobject (j)); Goodslist.add (Goodsbean); } } Catch(jsonexception e) {e.printstacktrace (); } } }}
Goodsbean Entity classes:
/*** Created by Administrator on 2015/7/24.*/ Public classGoodsbean {Private intstock_id;//Product ID PrivateString Goods_name;//Product Name Private intQty//the star of the product Private BooleanIs_choose;//whether to select Private DoublePrice//Unit Price PrivateString goods_img;//Image Link PrivateString extend;//Image Development Private intStock_num;//Number of goods Private DoubleGoodstotalprice;//Total Merchandise Private DoubleRate//Commodity tax rate PublicString Getgoods_name () {returnGoods_name; } Public voidsetgoods_name (String goods_name) { This. Goods_name =Goods_name; } Public intGetqty () {returnQty; } Public voidSetqty (intqty) { This. Qty =Qty; } Public BooleanIs_choose () {returnIs_choose; } Public voidSetis_choose (Booleanis_choose) { This. Is_choose =Is_choose; } Public DoubleGetPrice () {returnPrice ; } Public voidSetprice (DoublePrice ) { This. Price =Price ; } PublicString getgoods_img () {returngoods_img; } Public voidsetgoods_img (String goods_img) { This. goods_img =goods_img; } PublicString getextend () {returnextend; } Public voidSetextend (String extend) { This. Extend =extend; } Public intGetstock_num () {returnStock_num; } Public voidSetstock_num (intstock_num) { This. Stock_num =Stock_num; } Public DoubleGetgoodstotalprice () {returnGoodstotalprice; } Public voidSetgoodstotalprice (DoubleGoodstotalprice) { This. Goodstotalprice =Goodstotalprice; } Public Doublegetrate () {returnRate ; } Public voidSetrate (DoubleRate ) { This. Rate =Rate ; } Public intgetstock_id () {returnstock_id; } Public voidSETSTOCK_ID (intstock_id) { This. stock_id =stock_id; } PublicGoodsbean (Jsonobject jsonobject) { This. stock_id = Jsonobject.optint ("stock_id"); This. Goods_name = jsonobject.optstring ("Goods_name"); This. Qty = jsonobject.optint ("qty"); This. Is_choose = Jsonobject.optboolean ("Is_choose"); This. Price = Jsonobject.optdouble ("Price"); This. goods_img = jsonobject.optstring ("goods_img"); This. Extend = jsonobject.optstring ("Extend"); This. Stock_num = Jsonobject.optint ("Stock_num"); This. Goodstotalprice = jsonobject.optdouble ("Goodstotalprice"); This. Rate = jsonobject.optdouble ("rate"); } PublicGoodsbean () {} @Override PublicString toString () {return"Level:" + qty + "+" tax rate: "+Rate ; }}View Code
Online parsing json for Android