0

I wish to append a List<Beans> in a HTTP Post request.

While making use of Apache HTTPClient, I am unable to do the same.

This is what I wish to do:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);

nameValuePairs.add(new BasicNameValuePair("dailySalesList",beanList));

post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

However, the BasicNameValuePair seems to take only 2 Strings as argument. I wish to utilize it so that I can use a String - for identification and Object - to pass a List. Basically a functionality similar to using a Map.

Any pointers on how it can be done using Apache HTTPClient ?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
1985percy
  • 433
  • 1
  • 5
  • 8

2 Answers2

1

The NameValuePair in this context is intended for sending simple text parameters as queries in the URL (the ?foo=1&bar=2 parameters you see sometimes). The best way to send something more complex like a list is to serialize it in an interchange format like XML or JSON and then send it as the request body.

jonvuri
  • 5,738
  • 3
  • 24
  • 32
0

Thanks Kiyura for pointing me in the right direction, to make use of JSON.

I made use of the GSON java library for conversion from List to JSON representation.

Its a pretty library in case you want to use JSON without manually converting Generic Types.

The gson doc is also a good reference.

1985percy
  • 433
  • 1
  • 5
  • 8