0

I have an app that sends an object to an api via HttpPost. I have some sample such:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("Destination", destination));
nameValuePairs.add(new BasicNameValuePair("Description", description));
nameValuePairs.add(new BasicNameValuePair("CreatorName ", myName));

In this object, I have an array of "User" objects, which has a DisplayName and Phone. How can I send this array through HttpPost? The following is not recognized by the server (where the quotes are escaped):

nameValuePairs.add(new BasicNameValuePair("Users", "[{"Destination":"St.Louis", "Phone":"1234"}]"));

Nadim Hossain Sonet
  • 1,630
  • 1
  • 16
  • 23
user1145998
  • 71
  • 1
  • 12

3 Answers3

1

Simplest way is to convert the Array as String (or JSON string). Then encode it with your server after passing as entity.

wehpogi
  • 121
  • 4
1

try this

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppostreq = new HttpPost(wurl);

StringEntity se = new StringEntity(nameValuePairs.toString());
se.setContentType("application/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));

httppostreq.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppostreq);
Marian Nasry
  • 821
  • 9
  • 22
Nadim Hossain Sonet
  • 1,630
  • 1
  • 16
  • 23
0

I think this code should work.

nameValuePairs.add(new BasicNameValuePair("user[0]", gson.toJson(users.get(0))));
nameValuePairs.add(new BasicNameValuePair("user[1]", gson.toJson(users.get(1))));