【问题标题】:Android Retrofit: content type as application/x-www-form-urlencodedAndroid Retrofit:内容类型为 application/x-www-form-urlencoded
【发布时间】:2015-04-02 04:22:18
【问题描述】:

对 android 开发相当陌生。我正在尝试使用改造来发送发布请求。在我的改造日志中,我看到了

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

我发现只有在我使用内容类型时请求才会起作用:

application/x-www-form-urlencoded

我搜索了谷歌并没有找到明确设置内容类型的明确方法。有人知道怎么做吗?

【问题讨论】:

    标签: android post retrofit


    【解决方案1】:

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

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

    在有方法声明的接口中。

    【讨论】:

      【解决方案2】:

      在改造 2 中有点不同:

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

      【讨论】:

      • @FormUrlEncoded 无法与GET 方法一起使用,同样@Field
      【解决方案3】:

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

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

      【讨论】:

      • 这是我正在做的事情:@FormUrlEncoded @POST("/login/post") public void authenticateUserWithUserName(@Field("email") String email,@Query("password") String password ,Callback> 回调);我收到一条错误消息:
      • 表单编码方法必须至少包含一个@Field。
      • 我将第二个 @Query 更改为 Field.Worked 太棒了!
      • 这是我的代码:@FormUrlEncoded @POST("/flikad/Api/User/shoppersLogin") Call userLognin(@Field("email") String email, @Field("password ") 字符串密码);我仍然收到错误:Response{protocol=http/1.1, code=400, message=Bad Request, url=http://..}
      • @MayuraDevani 你是如何解决错误请求问题的。我也遇到同样的错误
      猜你喜欢
      • 2021-02-03
      • 1970-01-01
      • 2020-04-21
      • 1970-01-01
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-28
      相关资源
      最近更新 更多