0

I need to send an array to a server but I cannot seem to find the correct formatting for it.

Here is the php equivalent that I need to send

array(
      'interest' => 1,
      'charity' => 590,
      'items' => array(
        array(
          'cart_item_id' => 11197,
          'message' => '',
          'aid' => 174
        ),
      ),
    );

I am trying to get send my data in nameValuePairs - here is what I am trying

List<NameValuePair> nameValuePairs  =   new ArrayList<NameValuePair>();
    if (interest == true) {
        nameValuePairs.add(new BasicNameValuePair("interest", "1"));                
    } else {
        nameValuePairs.add(new BasicNameValuePair("interest", "0"));    
    }
    nameValuePairs.add(new BasicNameValuePair("charity", String.valueOf(charityId)));
    int len = cartItems.size();
    for (int i = 0; i < len; ++i) {
         nameValuePairs.add(new BasicNameValuePair("items[" + String.valueOf(i) + "]" + "[cart_item_id]", String.valueOf(cartItems.get(i).cartItemId)));
        nameValuePairs.add(new BasicNameValuePair("items[" + String.valueOf(i) + "]" + "[message]", cartItems.get(i).message));
        nameValuePairs.add(new BasicNameValuePair("items[" + String.valueOf(i) + "]" + "[aid]", String.valueOf(cartItems.get(i).addressId)));
    }

This is incorrect tho. As always any help is much appreciated

Edit

For clarification this is what my app sends to the server

[interest=0, charity=1878, items[0][cart_item_id]=14498, items[0][message]=, items[0][aid]=315, items[1][cart_item_id]=14499, items[1][message]=, items[1][aid]=318]

but that is incorrect

2
  • I think you create problem in for loop,First check name and value proper as per you wish using System.out.prinln();,Mind that you must send string value to server Commented Jan 10, 2012 at 14:28
  • The app runs fine. It sends the details to the server but they are formatted incorrectly. I am sending strings Commented Jan 10, 2012 at 14:31

3 Answers 3

2

You should use the HashMap in the package java.util. Have a look to the API docs for more details ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the input but I was looking how to format the array really. I will look into hash map as it does seem like the correct class to use but it doesn't help me with my question of how the array should be formatted.
1

use a HashMap<Object,Object> , and then when you want to add the sub array :

array(
          'cart_item_id' => 11197,
          'message' => '',
          'aid' => 174
)

just add a new HashMap<String,String> into your Super HashMap<Object,Object> : the logic will be something like this:

HashMap parentMap = new HashMap(); HashMap subMap = new HashMap();

// put params into parentMap
parentMap.put("key1", "this is a string value");
parentMap.put("key2", 45);
parentMap.put("key3", new Date());

subMap.put("card_item_id", ""+11197);
subMap.put("message", "this is a message from subMap");
parentMap.put("items", subMap);

//get the subMap from the parentMap like this 
HashMap<String, String> copySubMap = (HashMap<String, String>) parentMap.get("items");

hope that you got the idea of how formatting your array :)

9 Comments

That has made it a lot clearer thank you. However that code does not compile. I am getting the error The method add(String, String) is undefined for the type HashMap<Object,Object> is should be put instead of add :)
i've just write the code without using eclipse ide , so the problem you got is just a cast or something else. the essential is that you've got the idea of how to resolve your problem . glad to hear it :)
Yes that it leaves me with the problem of encoding a hashmap now
I think I was a little quick with awarding the answer. I can't get an the numberous subMaps in the parentMap. I am logging parentMap.toString() that just shows 1 array of items
Ok so it turns out the request for formatted correctly. The problem being that the server will only accept "&" between each entry rather than "," that I have been sending. Any help with that problem will be appreciated
|
0

Java class HashMap should do it. Uses key-value pair concept.

Here is implementation example, http://www.javadeveloper.co.in/java-example/java-hashmap-example.html

1 Comment

Thanks for the input but I was looking how to format the array really. I will look into hash map as it does seem like the correct class to use but it doesn't help me with my question of how the array should be formatted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.