多部分JSON POST请求

时间:2013-06-06 14:41:55

标签: java json web-services rest

请阅读我的帖子:

我需要使用以下参数将图像发布到JSON WS:

Content-Type: multipart/related; boundary="foo_bar_baz"
Content-Length: {number_of_bytes_in_entire_request_body} -- Check your rest client                   API's. Some would automatically determine content-length at runtime. Eg. jersey client api.
 --foo_bar_baz
Content-Type: application/json;
{
"filename": "cloudx.jpg"
}
--foo_bar_baz
Content-Type: image/jpeg
{JPEG data}
--foo_bar_baz--

我正在构建Android应用程序,我需要编写请求将图像发送到上面的WS。我正在寻找一段时间,我找不到很好的资源来研究这个问题。

1 个答案:

答案 0 :(得分:1)

以下可能会给你基本的想法

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(<URI>);
HttpEntity entity = new FileEntity(file,ContentType.MULTIPART_FORM_DATA);
//there are other types of entities and content types too check documentation
req.setEntity(entity);
HttpResponse response = httpClient.execute(req);