1

In my Android application, I need to send following JSON array to server using name value pair. This is the following json response and I need to send insertedIDs array to the sever.

{
    "message": "Deal was successfully done",
    "insertedIDs": [
        {
            "deal_id": "579",
             "name": "zzzz"

        },
        {
            "deal_id": "580",
             "name": "zzzz"
        }
    ],
    "status": "1"
}

this is the following code to communicate to server.

httpClient = new DefaultHttpClient();
resHandler = new BasicResponseHandler();
httpPost = new HttpPost(payment);
nameValuePairs = new ArrayList<NameValuePair>(4);

nameValuePairs.add(new BasicNameValuePair("loggedin_id",loggedin_id));
nameValuePairs.add(new BasicNameValuePair("amount",amount));
nameValuePairs.add(new BasicNameValuePair("payment_card_id",id));
nameValuePairs.add(new BasicNameValuePair("insertedIDs", ????)); // need to add the json array here.

try {
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    jsonResponse = httpClient.execute(httpPost, resHandler);
    Log.e("payment response", jsonResponse);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182
  • why not generating also a json and send it to the server? Have a look at gson (its a good json lib and many tutorials out there) – A.S. Dec 19 '13 at 13:45
  • and what is your question? – Philipp Sander Dec 19 '13 at 13:48
  • @A.S.: He's looking for a way how to put an array into the POST-parameters of a HTTP request. JSON is not the right thing here. – Daniel S. Dec 19 '13 at 13:48
  • @PhilippSander: The question is how to put an array of parameters into UrlEncodedFormEntity. – Daniel S. Dec 19 '13 at 13:48
  • Why not, you can maka a json out of array and put it in a post param? `new BasicNameValuePair("insertedIDs",new GsonBuilder.create().toJson(mArray));` – A.S. Dec 19 '13 at 13:49
  • @A.S.: That is certainly correct, but he is developing on the client side and the server side is most likely fixed and 3rd party, so he can't change how the server handles messages, thus if it's not accepting JSON, you have no chance using JSON. – Daniel S. Dec 19 '13 at 13:50
  • Ok so please tell me what format this server is accepting? show me the expected format? – A.S. Dec 19 '13 at 13:51
  • 1
    @A.S.: OK, maybe I was too fast. @ murali_ma: do you also develop the server side part of this communication or can you talk to the people doing it? – Daniel S. Dec 19 '13 at 13:55
  • @DanielS.,I just talk to the people do it. I am just doing client(android) development. – M.A.Murali Dec 19 '13 at 13:57
  • 1
    murali_ma, generally, this is possible. But how to do it exactly depends on the people developing the server side. You need to negotiate that with them. If they can't tell how, then ask them to give you a working example. Otherwise, you can propose them solutions like A.S.'s with JSON or the one here with "[i]" in the keys: http://stackoverflow.com/questions/12365832/setting-parameters-in-http-post . I think A.S.'s suggestion is best. – Daniel S. Dec 19 '13 at 14:07
  • 1
    @murali_ma choose POST variables or JSON data but don't mix the 2! – meda Dec 19 '13 at 14:09

0 Answers0