我正在尝试使用Retrofit2在服务器上发送文件.我根据文档做了一切,但总是得到400服务器错误.
我试图这样做:
RequestBody body =
RequestBody.create(MediaType.parse("image/png"), photo);
//..........
@Multipart
@POST(ADD_PHOTO)
Observable<HPSPhotoResponse>
addPhoto(@Part("file") RequestBody file);
Run Code Online (Sandbox Code Playgroud)
......并且像这样:
MultipartBody.Part part = MultipartBody.Part.createFormData("file", "file", body);
//...........
@Multipart
@POST(ADD_PHOTO)
Observable<HPSPhotoResponse>
addPhoto(@Part("file") MultipartBody.Part files);
Run Code Online (Sandbox Code Playgroud)
无所谓 结果始终相同"多部分请求:不存在所需的MultipartFile参数'文件'" - 服务器响应.
我认为服务器上的Spring工作不好,但是我在Swift(iOS)上做了相同的代码而且它有效!这里服务器看到这个'文件'部分.
Alamofire.upload(method, endpoint, headers: headers,
multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(fileURL: self.filePath!, name: "file")
}
Run Code Online (Sandbox Code Playgroud)
现在我希望它可以在Android上使用Retrofit.但我甚至查看了Retrofit请求的日志,实际上我在日志中看不到任何"文件"文本.
这有什么问题?
服务参数:
{"id":"1","name":"vishal","image/file":""}
Run Code Online (Sandbox Code Playgroud)
那时我API的Retrofit
@Multipart
@POST("webservice")
Call<SignUpResp> loadSignupMultipart(@Part("description") RequestBody description, @Part MultipartBody.Part file, @QueryMap HashMap<String, String> params);
Run Code Online (Sandbox Code Playgroud)
@Body class<UploadwithImage>{
"methodName":"submitLevel1Part2Icon",
"userid":"150",
"headerData":{
"fiction":{
"icon_type":"1",
"icon_id":"3"},
"nonfiction":{
"icon_type":"2",
"icon_id":"4"},
"relation":{
"icon_type":"3",
"icon_id":"0",
"name":"Ronak",
"relative_image":"<File>",
"relation_id":"3"},
"self":{
"icon_type":"4",
"icon_id":"0"}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试这个 API
@Multipart
@POST("webservice")
Call<SubmitLevel1Part2IconResp> loadLevel1halfIconswithImage(@Part("description") RequestBody description, @Part MultipartBody.Part file, @Body UploadwithImage uploadImage);
Run Code Online (Sandbox Code Playgroud)
/**
* code for multipart
*/
// create RequestBody instance from file
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), fileUpload);
// MultipartBody.Part is …Run Code Online (Sandbox Code Playgroud)