【问题标题】:trying to put JSONObject inside @Field in POST Api but not working试图将 JSONObject 放在 POST Api 中的 @Field 内但不工作
【发布时间】:2018-09-29 12:05:35
【问题描述】:
  @Headers("Accept: application/json")
    @POST("/api/v1/search/filter")
    @FormUrlEncoded fun searchFilter(@Field("leaf_id") leaf_id: Int,
                                     @Field("lang") lang: String,
                                     @Field("token") token: String,
                                     @Field("ud_id") ud_id: String,
                                     @Field("min_price") min_price: Float,
                                     @Field("max") max: Float,
                                     @Field("page") page: Int,
                                     @Field("per_page") per_page: Int,
                                     @Field("properties") properties:JSONObject): Observable<RespProductsInCatg>

@Field("properties") 中,我正在尝试发送 json 对象但响应失败。 在 Postman 中,它返回正确的响应

【问题讨论】:

  • 您在 POSTMAN 中尝试过,那里运行正常吗?

标签: android post kotlin retrofit2


【解决方案1】:

尝试使用数据类作为要在属性字段中传递的参数。 然后传递该数据类的对象,并使用@Body 而不是@Field 作为属性。

【讨论】:

  • 我需要在我的请求中添加名为“properties”的JsonObject,我该怎么做?
  • 类似这样的 { "Марка" : [ "ЭУД 72700" ] }
  • 只需创建一个kt文件,如Request model.kt
  • 然后使用数据类Properties(
  • 嗨,我做过类似的事情。Hipe 你了解代码。数据类 ResponseResult( var 响应:ArrayList ) 数据类 ResponseList( var question: Int = 0, var response : Int = 0)
【解决方案2】:

尝试将整个请求创建为对象并添加与 @Body 参数相同的参数,而不使用 @Field 值。

类似

data class Request(var leaf_id:Int,
               var lang: String,
               .
               .
               .
               var properties: List<Property>);

 data class Property(var param1:Int, .... );

构造请求对象并尝试将其添加为@Body 参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-17
    • 2015-10-29
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 2018-02-06
    • 2014-06-23
    • 2018-01-17
    相关资源
    最近更新 更多