如何在JavaHTTPPOST请求的数据部分添加数组?

如何在JavaHTTPPOST请求的数据部分添加数组?,java,apache,http-post,multipart,Java,Apache,Http Post,Multipart,我正在使用ApacheHttpPost和MultipartEntity用Java编写一个HttpPost请求。在请求的数据部分,我可以使用addPart(name,StringBody)添加简单部件。但是,我需要添加身体部位,它是一个值数组。我该怎么做?curl请求的示例如下: curl -k -H "Content-Type: application/json" --data '{ "name":"someName", "email":"noone@nowhere.com", "proper

我正在使用Apache
HttpPost
MultipartEntity
用Java编写一个HttpPost请求。在请求的数据部分,我可以使用
addPart(name,StringBody)
添加简单部件。但是,我需要添加身体部位,它是一个值数组。我该怎么做?curl请求的示例如下:

curl -k -H "Content-Type: application/json" --data '{ "name":"someName", 
"email":"noone@nowhere.com", "properties" : { "prop1" : "123", "prop2" : "abc" }}' 
-X POST 'https://some.place.com/api/test'
在Java中,我可以像这样创建请求,但我需要知道如何创建“properties”数组值,因为StringBody用于单个值:

HttpPost httpPost = new HttpPost(newAdultUrl);
MultipartEntity entity = new MultipartEntity();
entity.addPart("name", new StringBody("someName"));
entity.addPart("email", new StringBody("noone@nowhere.com"));
entity.addPart("properties", new ??? );
httpPost.setEntity(entity);

谢谢你的帮助

StringBody
中传递一个将数组转换为json的方法。
比如说


StringBody
中,传递一个将数组转换为json的方法。
比如说


以下是一种使用StringEntity而不是MultipartEntity的方法:

HttpPost httpPost = new HttpPost(newUrl);
String jsonData = <create using your favorite JSON library>;
StringEntity entity = new StringEntity(jsonData);
httpPost.setEntity(entity);
HttpPost-HttpPost=newhttppost(newUrl);
字符串jsonData=;
StringEntity实体=新的StringEntity(jsonData);
httpPost.setEntity(实体);

如果有多个部件,我希望看到使用多个部件的答案,但这将完成工作。

以下是一种使用StringEntity而不是多个部件的方法:

HttpPost httpPost = new HttpPost(newUrl);
String jsonData = <create using your favorite JSON library>;
StringEntity entity = new StringEntity(jsonData);
httpPost.setEntity(entity);
HttpPost-HttpPost=newhttppost(newUrl);
字符串jsonData=;
StringEntity实体=新的StringEntity(jsonData);
httpPost.setEntity(实体);

我希望看到使用MultipartEntity(如果有)的答案,但这将完成工作。

我做了与Roberg类似的事情,但对我来说,在键的末尾添加了
[]
,例如:

 entity.addPart("properties[]", new StringBody("value 1"));
 entity.addPart("properties[]", new StringBody("value 2"));
 entity.addPart("properties[]", new StringBody("value 3"));

我做了与Roberg类似的事情,但对我来说,在键的末尾添加了
[]
,例如:

 entity.addPart("properties[]", new StringBody("value 1"));
 entity.addPart("properties[]", new StringBody("value 2"));
 entity.addPart("properties[]", new StringBody("value 3"));

我也在努力解决这个问题,并且能够找到答案

entity.addPart("properties[prop1]", new StringBody("123");
entity.addPart("properties[prop2]", new StringBody("abc");

我也在努力解决这个问题,并且能够找到答案

entity.addPart("properties[prop1]", new StringBody("123");
entity.addPart("properties[prop2]", new StringBody("abc");

对我来说,
properties[]
properties
对我不起作用

为每个数组指定数字的操作如下所示

 entity.addPart("properties[0]", new StringBody("value 1");
 entity.addPart("properties[1]", new StringBody("value 2");
 entity.addPart("properties[2]", new StringBody("value 3");

对我来说,
properties[]
properties
对我不起作用

为每个数组指定数字的操作如下所示

 entity.addPart("properties[0]", new StringBody("value 1");
 entity.addPart("properties[1]", new StringBody("value 2");
 entity.addPart("properties[2]", new StringBody("value 3");

您应该使用JSON库来为您进行编码,它更干净、更灵活;一个简单的方法是Gson。好的,我找到了一种方法。我可以使用StringEntity而不是MultipartEntity,并从所需的各种数据字段创建我自己的JSON字符串(我们在堆栈中使用了Gson和JSON simple)。但是,如果有人知道如何使用我上面的代码示例中的MultipartEntity方法,我仍然更喜欢这种方法;一个简单的方法是Gson。好的,我找到了一种方法。我可以使用StringEntity而不是MultipartEntity,并从所需的各种数据字段创建我自己的JSON字符串(我们在堆栈中使用了Gson和JSON simple)。但是,如果有人知道如何使用我上面的代码示例中的MultipartEntity方法,我仍然希望使用这种方法,即将数组作为字符串而不是数组传递。我需要“properties”:{“key”:“value”},而不是“properties”:“{“key”:“value”}”。试着创建一个InputStream并使用org.apache.http.entity.mime.content.InputStreamBody实际上我误解了你的问题,现在我只是猜测将数组作为字符串而不是数组传递。我需要“属性”:{“键”:“值”},而不是“属性”:{“键”:“值”}。尝试创建一个InputStream并使用org.apache.http.entity.mime.content.InputStreamBody实际上我误解了你的问题,现在我只是在猜测