【问题标题】:Retrofit GET request with JSON parameters使用 JSON 参数改造 GET 请求
【发布时间】:2020-06-14 02:51:56
【问题描述】:

我正在尝试使用 Retrofit 我的自定义对象从 Parse Server 获取。

http 请求是:

https://MY.SERVER./1/classes/Fiestas?where={"Epoca":{"__type":"Pointer","className":"Epocas","objectId":"myObjectId"}}

所以我有这个界面:

@GET("Fiestas?where={\"Epoca\":{\"__type\":\"Pointer\",\"className\":\"Epocas\",\"objectId\":\"{epoca_id}\"}}")
fun getFiestaByEpocaId(@Path("epoca_id") epoca_id: String): Deferred<JSONResponse>

但我收到此错误:

java.lang.IllegalArgumentException: URL query string "where={"Epoca":{"__type":"Pointer","className":"Epocas","objectId":"{epoca_id}"}}" must not have replace block. For dynamic query parameters use @Query.
    for method FiestaApi.getFiestaByEpocaId

有人知道在 kotlin 的 GET 改造请求中发送 JSON 的任何解决方案吗?

谢谢!

【问题讨论】:

    标签: kotlin retrofit2


    【解决方案1】:

    你可以试试这个:

    @GET("Fiestas")
    fun getFiestaByEpocaId(
        @Query("where") jsonString: String
    ): Call<JSONObject>
    

    你的 json 对象是:

    val jsonString = "{\"__type\":\"Pointer\",\"className\":\"Epocas\",\"objectId\":\"{epoca_id}\"}}"
    

    并在请求方法之前调用jsonString.replace("\","")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 2016-06-16
      • 2019-04-27
      相关资源
      最近更新 更多