如何使用 Retrofit 在多部分表单请求中发送 POJO 对象?
How can I send a POJO object in a Multipart-form Request using Retrofit?
我正在尝试使用 Retrofit
在 Multipart-form
请求中发送以下内容:
{ "attachment": {
"file_cache": "....."
"original": "upload/image.png",
"versions": {
"small": "uploads/small_image.png"
}
},
"content": "",
"id": 1
}
我不知道这是否是我应该发送给 API 的正确请求,因为他们的文档真的很糟糕,但我能够使用 Chrome 开发工具来研究什么API 正在明智地接收请求以及它是如何响应的,它似乎接受了 JSON.
这是我观察到的照片:
他们的文档只说明 "attachment"
应该是一个对象。
是否可以在 multipart-form
请求中发送 POJO
对象?我的 REST 界面如下所示:
@Multipart
@POST("/v2/{type}/{id}/message.json")
void addMessage(@Path("type") String type,
@Path("id") int id,
@Part("content") String content,
@Part("attachment") MultipartTypedOutput multipartTypedOutput,
Callback<Post> callback);
发送 MultipartTypedOutput
无效,使用以下内容也无效:
addMessage(...., @Part("attachment") POJOObject object, ...);
关于如何实现这个的任何想法?
如果我尝试使用 Retrofit
.
发送 POJO
对象,我会得到状态 422
不可处理的实体
你试过 TypedFile 了吗?如果不试试这个方法,
@Multipart
@POST("/v2/{type}/{id}/message.json")
void addMessage(@Path("type") String type,
@Path("id") int id,
@Part("content") String content,
@Part("attachment") TypedFile photoFile,
Callback<Post> callback);
对于 TypedFile,您可以通过这种方式传递附件,
File attachmentFile = new File("upload/image.png");
TypedFile attachmentTypedFile = new TypedFile("image/*", photoFile);
我能够按照这个 link here.
解决这个问题
我不知道这一点,但为了发送 JSON
,您需要像这样设置您的 REST API Service
:
@Multipart
@POST("/v2/{type}/{id}/message.json")
void addMessage(@Path("type") String type,
@Path("id") int id,
@Part("content") String content,
@Part("attachment[file_cache]") String fileCache,
@Part("attachment[original]") String original,
@Part("attachment[versions][small]") String small,
Callback<Post> callback);
希望这对以后的其他人有所帮助。
我正在尝试使用 Retrofit
在 Multipart-form
请求中发送以下内容:
{ "attachment": {
"file_cache": "....."
"original": "upload/image.png",
"versions": {
"small": "uploads/small_image.png"
}
},
"content": "",
"id": 1
}
我不知道这是否是我应该发送给 API 的正确请求,因为他们的文档真的很糟糕,但我能够使用 Chrome 开发工具来研究什么API 正在明智地接收请求以及它是如何响应的,它似乎接受了 JSON.
这是我观察到的照片:
他们的文档只说明 "attachment"
应该是一个对象。
是否可以在 multipart-form
请求中发送 POJO
对象?我的 REST 界面如下所示:
@Multipart
@POST("/v2/{type}/{id}/message.json")
void addMessage(@Path("type") String type,
@Path("id") int id,
@Part("content") String content,
@Part("attachment") MultipartTypedOutput multipartTypedOutput,
Callback<Post> callback);
发送 MultipartTypedOutput
无效,使用以下内容也无效:
addMessage(...., @Part("attachment") POJOObject object, ...);
关于如何实现这个的任何想法?
如果我尝试使用 Retrofit
.
POJO
对象,我会得到状态 422
不可处理的实体
你试过 TypedFile 了吗?如果不试试这个方法,
@Multipart
@POST("/v2/{type}/{id}/message.json")
void addMessage(@Path("type") String type,
@Path("id") int id,
@Part("content") String content,
@Part("attachment") TypedFile photoFile,
Callback<Post> callback);
对于 TypedFile,您可以通过这种方式传递附件,
File attachmentFile = new File("upload/image.png");
TypedFile attachmentTypedFile = new TypedFile("image/*", photoFile);
我能够按照这个 link here.
解决这个问题我不知道这一点,但为了发送 JSON
,您需要像这样设置您的 REST API Service
:
@Multipart
@POST("/v2/{type}/{id}/message.json")
void addMessage(@Path("type") String type,
@Path("id") int id,
@Part("content") String content,
@Part("attachment[file_cache]") String fileCache,
@Part("attachment[original]") String original,
@Part("attachment[versions][small]") String small,
Callback<Post> callback);
希望这对以后的其他人有所帮助。