将multipartRequest发送到服务器时遇到问题。
RequestBody file = RequestBody.create(MediaType.parse("application/json"), myFile);
return apiService.updateProfile2(token, file);
@Multipart
@POST("/profile/update")
Call<RegistrationResponse> updateProfile2(@Header(value = "X-AUTH-TOKEN") String toke, @Part(value = "json") RequestBody json);
问题:
请求正文为空,当它来到服务器
答案 0 :(得分:1)
Uploading files with Retorfit 2 and Multipart
http://www.codejava.net/java-se/networking/upload-files-by-sending-multipart-request-programmatically 这是一篇包含发送multipart的代码的好文章。这对我有用。
MultipartUtility multipart = new MultipartUtility(requestURL, charset);
multipart.addHeaderField("User-Agent", "CodeJava");
multipart.addHeaderField("Test-Header", "Header-Value");
multipart.addFormField("description", "Cool Pictures");
multipart.addFormField("keywords", "Java,upload,Spring");
multipart.addFilePart("fileUpload", uploadFile1);
multipart.addFilePart("fileUpload", uploadFile2);
List<String> response = multipart.finish();
System.out.println("SERVER REPLIED:");
for (String line : response) {
System.out.println(line);
}
答案 1 :(得分:0)
首先,您是否尝试将mediaType“application / json”发送为@Multpart,如果您想发送多部分文件,那么您是否需要更改代码中的某些内容。
这样的事情:
@Multipart
@POST("/profile/update")
Call<RegistrationResponse> updateProfile2(
@Header(value = "X-AUTH-TOKEN") String toke,
@Part("myfile\"; filename=\"image.png\" ") RequestBody file);
将呼叫方法更改为:
RequestBody file = RequestBody.create(MediaType.parse("multipart/form-data"), myFile);
return apiService.updateProfile2(token, file);