博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android的在线解析Json
阅读量:5319 次
发布时间:2019-06-14

本文共 6189 字,大约阅读时间需要 20 分钟。

 前提是引用Volley的异步机制,引入它的jar包

 

public void getJSONByVolley() {    //mContext为上下文,        RequestQueue requestQueue = Volley.newRequestQueue(mContext);        String JSONDataUrl = "http://sjshop.easyder.com/app/order_index/getCart?buyer_id=511";        final ProgressDialog progressDialog = ProgressDialog.show(mContext, "正在下载", "Loading......");        progressDialog.setCancelable(true);//progressDialog可以取消        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(                Request.Method.GET,                JSONDataUrl,                null,                new Response.Listener
() { @Override public void onResponse(JSONObject response) { try { getcartJson(response); } catch (Exception e) { e.getMessage(); } if (progressDialog.isShowing() && progressDialog != null) { progressDialog.dismiss(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { System.out.println("sorry,Error"); } }); requestQueue.add(jsonObjectRequest); Log.i(TAG, "shopBeanList ---->解析完Json "); } //getcart的网络解析,参数-->JSONObject public void getcartJson(JSONObject jsonObject) {// 创建商店列表 List
shopBeanList= new ArrayList
(); JSONObject objectInfo = null; try { objectInfo = jsonObject.getJSONObject("info"); JSONArray arraygroup = objectInfo.getJSONArray("group"); for (int i = 0; i
"+ item); } } catch (JSONException e) { e.printStackTrace(); } if(mCallback!=null){ mCallback.getDat(shopBeanList); } }

实体类中添加了一个JsonObject的构造函数

ShopBean实体类:

/** * Created by Administrator on 2015/7/19 0019. */public class ShopBean {    private int seller_uid;//店铺ID    private String seller_name;//店铺名称    public List
goodsList;//商品信息列表 public String getSeller_name() { return seller_name; } public void setSeller_name(String seller_name) { this.seller_name = seller_name; } public int getSeller_uid() { return seller_uid; } public List
getGoodsList() { return goodsList; } public void setGoodsList(List
goodsList) { this.goodsList = goodsList; } public void setSeller_uid(int seller_uid) { this.seller_uid = seller_uid; } public ShopBean() { } //构造函数,参数为JSONObject型 public ShopBean(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 = new ArrayList<>(); for (int j = 0; j < jsonArray.length(); j++) { goodsBean = new GoodsBean(jsonArray.getJSONObject(j)); goodsList.add(goodsBean); } } catch (JSONException e) { e.printStackTrace(); } } }}

GoodsBean实体类:

/** * Created by Administrator on 2015/7/24. */public class GoodsBean {    private int stock_id;//商品ID    private String goods_name;//商品名称    private int qty;//商品的星级    private boolean is_choose;//是否选中    private double price;//单价    private String goods_img;//图片链接    private String extend;//图片拓展    private int stock_num;//商品数量    private double goodsTotalPrice;//商品总价    private double rate;//商品税率    public String getGoods_name() {        return goods_name;    }    public void setGoods_name(String goods_name) {        this.goods_name = goods_name;    }    public int getQty() {        return qty;    }    public void setQty(int qty) {        this.qty = qty;    }    public boolean is_choose() {        return is_choose;    }    public void setIs_choose(boolean is_choose) {        this.is_choose = is_choose;    }    public double getPrice() {        return price;    }    public void setPrice(double price) {        this.price = price;    }    public String getGoods_img() {        return goods_img;    }    public void setGoods_img(String goods_img) {        this.goods_img = goods_img;    }    public String getExtend() {        return extend;    }    public void setExtend(String extend) {        this.extend = extend;    }    public int getStock_num() {        return stock_num;    }    public void setStock_num(int stock_num) {        this.stock_num = stock_num;    }    public double getGoodsTotalPrice() {        return goodsTotalPrice;    }    public void setGoodsTotalPrice(double goodsTotalPrice) {        this.goodsTotalPrice = goodsTotalPrice;    }    public double getRate() {        return rate;    }    public void setRate(double rate) {        this.rate = rate;    }    public int getStock_id() {        return stock_id;    }    public void setStock_id(int stock_id) {        this.stock_id = stock_id;    }    public GoodsBean(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");    }    public GoodsBean(){    }    @Override    public String toString() {        return  "等级:" + qty + " " +                "税率:" + rate                ;    }}
View Code

 

转载于:https://www.cnblogs.com/shuyongzai/p/4679917.html

你可能感兴趣的文章
阿里巴巴面试之利用两个int值实现读写锁
查看>>
连续数字或英文字符文本强制换行
查看>>
DevExpress Carousel 设置水平滑动列表
查看>>
python总结--目录(转)
查看>>
成都同学聚会
查看>>
京华同学聚会
查看>>
@bzoj - 3750@ [POI2015] Pieczęć
查看>>
PHP定时任务
查看>>
浅谈性能测试
查看>>
Winform 菜单和工具栏控件
查看>>
jequery动态创建form
查看>>
CDH版本大数据集群下搭建的Hue详细启动步骤(图文详解)
查看>>
第六次java作业
查看>>
巧用Win+R
查看>>
浅析原生js模仿addclass和removeclass
查看>>
Python中的greenlet包实现并发编程的入门教程
查看>>
tweenlite使用说明
查看>>
ContentProvider数据访问详解
查看>>
java中遍历属性字段及值(常见方法)
查看>>
在iPhone应用中使用自定义字体
查看>>