android - Nested JsonArray parsing for Pinned ListView -
i'm using library make pinned section listview. api response below. need parse nested jsonarray problem i'm getting last object inside second loop jsonarray "products" such i'm getting same 2 list row item first section header instead of 2 different list row item . how parse nested jsonarray , add model class?
{    "all_cart_products": [      {        "seller_id": "3",        "seller_name": "avik roy",        "email": "nits.avik@gmail.com",        "seller_image": "http://104.131.83.218/makeoffer/upload/userimage/1491225073_ajeet_1000016806.jpg",        "products": [          {            "id": "7",            "product_user_id": "3",            "name": "rtutyikuyliou",            "desc": "ytuykloiu sadfvdsbhdf fvdn dfjntgfkmhygdd dfsdhbgdf asfsedgdrjn sfvdsbhdf sfaswg adaswfg adxav adcasvfs adaswfsde safds",            "quantity": "1",            "unit_price": "100.00",            "total_price1": 100,            "total_price": "100.00",            "itemimage": "http://104.131.83.218/makeoffer/upload/product/1489739799manab.jpg",            "totalquantity": "48"          },          {            "id": "1",            "product_user_id": "3",            "name": "product1",            "desc": "ewfdewfs dvdfsovdfjudf bvdfofidksopdfb dfbldfjbldfb fdbdfkljbdfb dfbfkdjbdfb dfbdfjklbdfb dfbldfjkdf bdflkbjdfbdf bdfkljbdfb dfbldfkjbdf bdfbldfjkbdf bdfklbjdfdf bdfbjdflbdf bdf",            "quantity": "1",            "unit_price": "200.00",            "total_price1": 200,            "total_price": "200.00",            "itemimage": "http://104.131.83.218/makeoffer/upload/product/1489737382manab.jpg",            "totalquantity": "50"          }        ]      },      {        "seller_id": "11",        "seller_name": "kartik  roy",        "email": "nits.kartik@gmail.com",        "seller_image": "http://104.131.83.218/makeoffer/upload/userimage/14902548541490254849295.jpg",        "products": [          {            "id": "12",            "product_user_id": "11",            "name": "titli ghh\n",            "desc": "uh ghd ff hg hi jj jf td ghh",            "quantity": "1",            "unit_price": "10.00",            "total_price1": 10,            "total_price": "10.00",            "itemimage": "http://104.131.83.218/makeoffer/upload/product/1490254269myprod.jpg",            "totalquantity": "5"          }        ]      }    ],    "total_qty": 1,    "ack": 1  }  private void preparedata(){          if(arraylist!=null )arraylist.clear();          stringrequest stringrequest = new stringrequest(request.method.post, "http://www.example.com/webservice/getallcartproducts",                  new response.listener<string>() {                      @override                      public void onresponse(string response) {                          string childimage="", childtitle="", childqty="", childprice="";                          system.out.println("sammy_response "+response);                          try {                              jsonobject jobj = new jsonobject(response);                              if(jobj.getint("ack")==1){                                  jsonarray jsonarray = jobj.getjsonarray("all_cart_products");                                  for(int i=0;i<jsonarray.length();i++){                                      jsonobject main = jsonarray.getjsonobject(i);                                      model = new model();                                        model.setheaderimage(main.getstring("seller_image"));                                       model.setheadertext(main.getstring("seller_name"));                                       model.setheaderid(main.getstring("seller_id"));                                      jsonarray jarr = main.getjsonarray("products");                                      for(int j=0; j<jarr.length(); j++){                                          jsonobject obj = jarr.getjsonobject(j);                                          /*childimage = obj.getstring("itemimage");                                          childtitle = obj.getstring("name");                                          childqty = obj.getstring("quantity");                                          childprice = obj.getstring("total_price");*/                                           model.setchildimage(obj.getstring("itemimage"));                                          model.setchildtitle(obj.getstring("name"));                                           model.setchildqty(obj.getstring("quantity"));                                           model.setchildprice(obj.getstring("total_price"));                                            arraylist.add(model);                                      }                                     /* model.setchildimage(childimage);                                      model.setchildtitle(childtitle);                                      model.setchildqty(childqty);                                      model.setchildprice(childprice);*/                                  }                              }                              madapter.notifydatasetchanged();                          } catch (jsonexception e) {                              // json error                              system.out.println("sammy_jsonerror "+e);                          }                      }                  }, new response.errorlistener() {              @override              public void onerrorresponse(volleyerror error) {                  if (error instanceof timeouterror) {                     toast.maketext(mainactivity.this,getstring(r.string.tooslow),toast.length_long).show();                  }else if (error instanceof noconnectionerror){                      toast.maketext(mainactivity.this,getstring(r.string.nointernet),toast.length_long).show();                  }else if (error instanceof authfailureerror) {                      system.out.println("sammy_authfailureerror "+error);                  } else if (error instanceof servererror) {                      system.out.println("sammy_servererror "+error);                  } else if (error instanceof networkerror) {                      system.out.println("sammy_networkerror "+error);                  } else if (error instanceof parseerror) {                      system.out.println("sammy_parseerror "+error);                  }              }          }) {              @override              protected map<string, string> getparams() {                  map<string, string> params = new hashmap<string, string>();                  params.put("user_id", "7");                  return params;              }          };            requestqueue requestqueue = volley.newrequestqueue(mainactivity.this);          requestqueue.add(stringrequest);          stringrequest.setretrypolicy(new defaultretrypolicy(                  10000,                  defaultretrypolicy.default_max_retries,                  defaultretrypolicy.default_backoff_mult));      }  
add dependancy
compile 'com.google.code.gson:gson:2.8.0'
and use parser class.
 public class myparserclass {  @com.google.gson.annotations.serializedname("all_cart_products") public list<allcartproducts> allcartproducts; @com.google.gson.annotations.serializedname("total_qty") public int totalqty; @com.google.gson.annotations.serializedname("ack") public int ack;  public static class products {     @com.google.gson.annotations.serializedname("id")     public string id;     @com.google.gson.annotations.serializedname("product_user_id")     public string productuserid;     @com.google.gson.annotations.serializedname("name")     public string name;     @com.google.gson.annotations.serializedname("desc")     public string desc;     @com.google.gson.annotations.serializedname("quantity")     public string quantity;     @com.google.gson.annotations.serializedname("unit_price")     public string unitprice;     @com.google.gson.annotations.serializedname("total_price1")     public int totalprice1;     @com.google.gson.annotations.serializedname("total_price")     public string totalprice;     @com.google.gson.annotations.serializedname("itemimage")     public string itemimage;     @com.google.gson.annotations.serializedname("totalquantity")     public string totalquantity;      public string getid() {         return id;     }      public void setid(string id) {         this.id = id;     }      public string getproductuserid() {         return productuserid;     }      public void setproductuserid(string productuserid) {         this.productuserid = productuserid;     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public string getdesc() {         return desc;     }      public void setdesc(string desc) {         this.desc = desc;     }      public string getquantity() {         return quantity;     }      public void setquantity(string quantity) {         this.quantity = quantity;     }      public string getunitprice() {         return unitprice;     }      public void setunitprice(string unitprice) {         this.unitprice = unitprice;     }      public int gettotalprice1() {         return totalprice1;     }      public void settotalprice1(int totalprice1) {         this.totalprice1 = totalprice1;     }      public string gettotalprice() {         return totalprice;     }      public void settotalprice(string totalprice) {         this.totalprice = totalprice;     }      public string getitemimage() {         return itemimage;     }      public void setitemimage(string itemimage) {         this.itemimage = itemimage;     }      public string gettotalquantity() {         return totalquantity;     }      public void settotalquantity(string totalquantity) {         this.totalquantity = totalquantity;     } }  public static class allcartproducts {     @com.google.gson.annotations.serializedname("seller_id")     public string sellerid;     @com.google.gson.annotations.serializedname("seller_name")     public string sellername;     @com.google.gson.annotations.serializedname("email")     public string email;     @com.google.gson.annotations.serializedname("seller_image")     public string sellerimage;     @com.google.gson.annotations.serializedname("products")     public list<products> products;      public string getsellerid() {         return sellerid;     }      public void setsellerid(string sellerid) {         this.sellerid = sellerid;     }      public string getsellername() {         return sellername;     }      public void setsellername(string sellername) {         this.sellername = sellername;     }      public string getemail() {         return email;     }      public void setemail(string email) {         this.email = email;     }      public string getsellerimage() {         return sellerimage;     }      public void setsellerimage(string sellerimage) {         this.sellerimage = sellerimage;     }      public list<products> getproducts() {         return products;     }      public void setproducts(list<products> products) {         this.products = products;     } } }      
Comments
Post a Comment