如何在get请求改型的URL中传递数据库查询?

时间:2019-06-13 18:04:12

标签: android kotlin retrofit2

我正在创建一个翻新的get请求,我需要传递数据库查询和Kotlin中URL中的一些空格字符(如“ $”)。但是我遇到了错误。

java.lang.IllegalArgumentException:URL查询字符串不能具有replace块。对于动态查询参数,请使用@Query。

这是我在邮递员中使用的URL,但在追溯文件中却无法使用

https://someURL.com?customParam=true&pageSize=100&query= $ filter =(drivercell eq'1111111119')$ orderby = creationTimedesc&withTotalPages = true

这是调用改造方法的代码

     val restServiceModel = DRestServiceModel.create()
     val model = restServiceModel.getTripsData("Basic bWs6SU9UMTIzNCM=", "application/json", "\$filter=(drivercell%20eq'1111111119')")

这是方法

     @GET("inventory/managedObjects?customParam=true&pageSize=100&{query}\$orderby=creationTimedesc&withTotalPages=true")
     fun getTripsData(@Header("Authorization") token: String, @Header("Content-Type") contentType: String, @Query("query", encoded = true) query : String): Single<TripsResponseModel>

请帮助我。

2 个答案:

答案 0 :(得分:1)

问题是您试图通过另一个查询提供Path参数时将Path参数放在查询的中间。您应该重新处理您的请求。尝试类似的东西:

@GET("inventory/managedObjects")
fun getTripsData(@Header("Authorization") token: String,
                 @Header("Content-Type") contentType: String,
                 @Query("customParam") customParam: Boolean?,
                 @Query("pageSize") pageSize: Int?,
                 @Query("query", encoded = true) query: String,
                 @Query("withTotalPages") withTotalPages: Boolean?): Single<TripsResponseModel>

并像这样使用它:

val model = restServiceModel.getTripsData("Basic bWs6SU9UMTIzNCM=", "application/json", true, 100, "your query_goes here", true)

这种方式应该可以工作。

答案 1 :(得分:0)

尝试将URL中的每个K(美元符号)字符替换为$,因为美元符号被视为URL中的特殊字符