0

How do I send more than one record at a time using json? This code is a sample I found online but I need to send say 100 objects or records at a time. The data comes from a database.

protected String doInBackground(String... args) {
        String name = inputName.getText().toString();
        String price = inputPrice.getText().toString();
        String description = inputDesc.getText().toString();

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("price", price));
        params.add(new BasicNameValuePair("description", description));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                "POST", params);
Will R.
  • 110
  • 1
  • 12
  • 1
    how about just using a loop and sending 100 of them? – ug_ Aug 13 '13 at 02:58
  • You're not actually sending json, you're just receiving it. In order to send more than one item, you'd need to know how the server expects more than one item. Currently, there doesn't appear a way for this to be done, other than following @ns47731's advice. – 323go Aug 13 '13 at 03:39
  • I created a loop but the data received by the server was inconsistent. records were missing on the receive side. the android part was ok. Someone gave me the idea of creating objects and adding them to an array. It worked awesome and went alot quicker on the server side. – Will R. Aug 16 '13 at 05:07
  • I was able to send 3800 database rows using jsonobjects within an array. Now I just have to polish the loop. – Will R. Aug 16 '13 at 05:11

2 Answers2

0

You can write the list of data to String, then send it to php url. In php json_decode to read the list of data;

public static class Entity{
        private String name;
        private String price;
        private String description;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPrice() {
            return price;
        }

        public void setPrice(String price) {
            this.price = price;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }
    }

    @Test
    public void send() throws Exception{
        ObjectMapper mapper = new ObjectMapper();
        List<Entity> list = new ArrayList<Entity>(); // get the list of Entity;
        String json = mapper.writeValueAsString(list); // write list as json

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://url.to.post");

        StringEntity entity = new StringEntity(json);
        post.setEntity(entity);

        HttpResponse response = client.execute(post);

        String result = EntityUtils.toString(response.getEntity());
        Object responseObject = mapper.readValue(result, Object.class);
    }

In order to use ObjectMapper you will need jackson-core-asl and jackson-mapper-asl in your libs.

Michael Mior
  • 28,107
  • 9
  • 89
  • 113
Larry.Z
  • 3,694
  • 1
  • 20
  • 17
0

You must create the JSONObject that represent the objects, and add it to the Request:

If you have this structure for example:

{[{name:"name1",price:"10"},{name:"name2",price:"15"}]}

 JSONArray elements=new JSONArray();
  JSONObject aux=new JSONObject().put("name", "name1");
    aux.put("price", 10);
    array.put(aux);

    aux=new JSONObject().put("name1", "name2");
    aux.put("price", 20);
    array.put(aux);

List<NameValuePair> parameters= new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("json", elements.toString()));
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpClient cliente = createHttpClient();
return cliente.execute(post);

And in the server you catch the parameter "json"

Wuilfor
  • 26
  • 2
  • This post helped me understand the most! I was able to grasp the idea of making an object and putting it into the array. making another object and adding it to the array etc.... etc... – Will R. Aug 16 '13 at 05:04