Android Retrofit:内容类型为application / x-www-form-urlencoded

时间:2015-02-03 03:44:44

标签: android post retrofit

Android开发方面的新手。我正在尝试使用改造来发送帖子请求。在我的改造日志中,我看到了

Content-Type: text/plain; charset=utf-8

我发现请求仅在我使用内容类型时才有效:

application/x-www-form-urlencoded

我搜索了Google,但没有找到明确设置内容类型的明确方法。有谁知道怎么做?

3 个答案:

答案 0 :(得分:82)

在您定义服务的班级中,修改相关方法以遵循以下模式:

@FormUrlEncoded
@POST/GET/PUT/DELETE("/your_endpoint")
Object yourMethodName(@Field("your_field") String yourField,...);

答案 1 :(得分:4)

翻新2有点不同:

@FormUrlEncoded
@POST/GET/PUT/DELETE("/your_endpoint")
Call<Task> createTask (@Field("your_field") String title); 

答案 2 :(得分:0)

您必须像这样添加请求标头:

@Headers("Content-Type: application/x-www-form-urlencoded")

在具有方法声明的接口中。