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 ?

2 Answers 2

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.

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

Comments

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.

Comments

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.