0

How to send array with POST like this

"survey_pages": [{
    "id": 1,
    "answers": [
        {"question": 4, "answer": true}, 
        {"question": 2, "answer": 1}, 
        {"question": 3, "answer": 1}, 
        {"question": 1, "answer": "2014-03-18T00:01:30"}
    ]
}]

Here id my code for sending data:

           List<NameValuePair> params = new ArrayList<NameValuePair>();

            params.add(new BasicNameValuePair("token", mSettings.getString("token", "")));
            params.add(new BasicNameValuePair("survey_pages[id]", _survey_id));
            //params.add(new BasicNameValuePair("id", _page_id));
            int listSize = _q_list.size();
            for (int i = 0; i<listSize; i++) {
                  question = question_list.get(i).id;
                  params.add(new BasicNameValuePair("answers[question]", question));
                  params.add(new BasicNameValuePair("answers[answer]", "true"));
            }               
1

2 Answers 2

1

Taken from here:

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 
Sign up to request clarification or add additional context in comments.

Comments

0

after creating name value pair you need just these two lines of code

 httppost.setEntity(new UrlEncodedFormEntity(params ));

        // Execute HTTP Post Request
 HttpResponse response = httpclient.execute(httppost);

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.