How To Post Array Of Values To WebSever

24 views
Skip to first unread message

Keen Student

unread,
Oct 17, 2013, 3:58:32 AM10/17/13
to codenameone...@googlegroups.com
I have an app which needs to post an array of values to a webserver.
The app  already has a version for blackberry which performs what i want using the following code:

   
for(int i=0;i<orders.size();i++)
            {
             Items item=new Items();
             item=(Items)orders.elementAt(i);
               params.append("item_id[]",item._itemId);
                 params.append("qty[]", String.valueOf(item.qty));
             params.append("price[]", item._realPrice);
            
          }
            byte[] postbytes=params.getBytes();
             httpConnection.setRequestProperty("Content-Length",String.valueOf(postbytes.length));
             OutputStream out=httpConnection.openOutputStream();
             out.write(postbytes);
             out.flush();

 This builds an array and attaches it to the array parameters of "item_id[]","qty[]", and "price[]". This is necessary because they ALL
belong to one order. I was trying to implement this in codenameone (for j2me)..and tried this:
 for(int i=0;i<orders.size();i++)
            {
                
            Hashtable item= (Hashtable)orders.elementAt(i);
            String item_ids=(String) item.get("id");
             String qtys=(String) item.get("qty");
              String prices=(String) item.get("real_price");
            r.addArgument("item_id[]",item_ids);
              r.addArgument("qty[]",qtys);
               r.addArgument("price[]",prices);
                         }
            
      InfiniteProgress ip=new InfiniteProgress();
      di=ip.showInifiniteBlocking();
       r.setDuplicateSupported(true);
      r.setDisposeOnCompletion(di);
      NetworkManager.getInstance().addToQueue(r);
     

However...the above code ONLY posts the LAST value in the vector and discards its predecessors. I need help on 
ensuring all the values in the vector get appended and posted in "A SINGLE" request in order to enable them share a single order id. Thanks

Shai Almog

unread,
Oct 17, 2013, 2:19:51 PM10/17/13
to codenameone...@googlegroups.com
We only support one argument of a given type, this is a rather rare case.
Often developers just split the arguments with a delimiter such as ; and use split(";") on the server to get an array.

If you insist you can always use addArgumentNoEncoding and in the value append everything you need but you will need to encode everything yourself.
Reply all
Reply to author
Forward
0 new messages