嗨,我主要是一名 Android 开发人员,对 http 协议一无所知,非常感谢您的帮助!
我正在尝试对此进行 api 调用:http ://api.haikutest.com/developer/apidoc/assignments.html我在设备上发布文件。
他们给出了一个样本请求:
POST /api/assignments/3/submit
message[subject]=Subject+of+the+message&message[body]=Some+long+text&message[sent_at]=2013-07-05&message[draft]=0&message[assignment_id]=3&files[][file]=%23%3CFile%3A0x007ff59c9c1de8%3E&files[][filename]=test.pdf
在描述状态中,文件参数是:
文件数组,每个元素包含文件名和 base64 编码文件的哈希
这是我现在正在尝试的代码(注意:我正在使用 Robospice + Spring for Android):
File file = new File(Environment.getExternalStorageDirectory() +
"/DCIM/Camera/1357279228432.jpg");
Base64InputStream stream = new Base64InputStream(new FileInputStream(file), 0);
HttpHeaders headers = new HttpHeaders();
headers.add("x-haiku-auth", HaikuAPI.getAuthHeader());
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<String, Object>();
parts.add("files[][file]", parts);
HttpEntity<Object> request = new HttpEntity<Object>(stream, headers);
UriComponentsBuilder builder = UriComponentsBuilder
.fromUriString(
"https://" + HaikuAPI.getDomain() + "/api/assignments")
.pathSegment(assignmentID + "", "submit")
.queryParam("message[subject]", "Assignment Completed")
.queryParam("message[body]", message)
.queryParam("message[assignment_id]", assignmentID)
.queryParam("files[][filename]", "test.jpg");
URI url = builder.build().toUri();
String response = getRestTemplate().postForObject(url, request, String.class);
return response;
但是我收到一条错误消息,说它无法将流序列化为 JSON。按照 API 文档,我是否必须将流作为参数?还有什么是实现这个的正确方法?如果我不必对用户设置文件大小限制,我会很高兴,因此流。虽然我已经让它处理以 Base64 编码并直接作为参数放置的小文件......
任何帮助是极大的赞赏!我绝对不会对小指针的评论或答案投反对票。
更新
更新了代码,但仍然收到另一条错误消息:
00:25:50.904 Thread-2066 An exception occurred during request network execution :Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [application/x-www-form-urlencoded]