如何向API发出数组POST请求?

时间:2014-03-27 05:27:47

标签: android

例如,我有一个API:

内容类型:application / json

{

"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"}]

我试图通过NameValue对列表来实现它,但是我不能这样做,因为没有NameValue Pair for Objects的构造函数。

主要问题是通过"阵列数组" to setEntity方法。

1 个答案:

答案 0 :(得分:0)

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("url");
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("token", LogedInUserDetails.getToken());
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httppost.setEntity(se);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httppost);
InputStream inputStream = httpResponse.getEntity().getContent();

你可以像这样添加。使用json对象和json数组创建所需的格式,并将实体设置为httppost。