【问题标题】:How to use @Body with @Field and @FormUrlEncoded?如何将@Body 与@Field 和@FormUrlEncoded 一起使用?
【发布时间】:2017-05-25 09:53:04
【问题描述】:
@FormUrlEncoded
@POST("/api/post")
Call<Response> createPost(
        @Header("auth") String auth,
        @Field("id") String id,
        @Field("title") String title,
        @Body ContentData content);

通过使用此代码,我收到错误消息“@Body 参数不能与表单或多部分编码一起使用。”。我现在该怎么办 ?我尝试将 Object 作为 String 发送,但也失败了。

【问题讨论】:

  • 使用 TypedInput..

标签: android http-post retrofit2 android-json square


【解决方案1】:

如果您要发送表单数据,请使用字段作为参数:

@Header("auth: YOUR_AUTH")
@FormUrlEncoded
@POST("/api/post")
Call<Response> createPost(
    @Field("id") String id,
    @Field("title") String title
);

如果您使用的不是表单数据,例如 application/json,请通过 @Body 发送您的正文:

@Header("auth: YOUR_AUTH")
@POST("/api/post")
Call<Response> createPost(@Body ContentData content);

这一切都取决于您的要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2021-04-01
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多