请问我如何使用 Retrofit 参数化此端点?
How would I parameterise this endpoint using Retrofit please?
假设我想查询一个 API,它有一个服务器方法来搜索包含特定文本的字段,如下所示:
http://server:2001/tms/xdata/Customer?$filter=contains(Name, 'Walker')
在使用 Kotlin 的 Android Studio 中,我会制作一个类似这样的界面:
@GET("/tms/xdata/Customer")
suspend fun fetchAllProductsContaining(@Query("name") searchTerm: String): CustomersResponse
如果我传递单个字符 'e' 作为 searchTerm 会给我:
http://server:2001/tms/xdata/Customer?name=e
但是我需要它看起来像这样:
http://server:2001/tms/xdata/Customer?%24filter=contains(lower(name)%2C'e')
因为我希望它不区分大小写,所以还要注意要搜索的文本必须用单引号括起来。
非常感谢你们提供的任何帮助。服务器使用TMS XData
写成Delphi
答案是使用QueryMap。
以这种方式调用函数:
suspend fun searchProductsRetrofit(searchTerm: String): List<Product> {
val options: MutableMap<String, String> = HashMap()
options["$filter"] = "contains(lower(Name),'$searchTerm')"
return retrofit().getProducts(options).value
}
在您的 API 界面中将其配置为使用传入的数据,如下所示:
@GET("/pmi/civic_amenity/CaProduct")
suspend fun getProducts(@QueryMap options: Map<String, String>): ProductsResponse
假设我想查询一个 API,它有一个服务器方法来搜索包含特定文本的字段,如下所示:
http://server:2001/tms/xdata/Customer?$filter=contains(Name, 'Walker')
在使用 Kotlin 的 Android Studio 中,我会制作一个类似这样的界面:
@GET("/tms/xdata/Customer")
suspend fun fetchAllProductsContaining(@Query("name") searchTerm: String): CustomersResponse
如果我传递单个字符 'e' 作为 searchTerm 会给我:
http://server:2001/tms/xdata/Customer?name=e
但是我需要它看起来像这样:
http://server:2001/tms/xdata/Customer?%24filter=contains(lower(name)%2C'e')
因为我希望它不区分大小写,所以还要注意要搜索的文本必须用单引号括起来。
非常感谢你们提供的任何帮助。服务器使用TMS XData
写成Delphi答案是使用QueryMap。 以这种方式调用函数:
suspend fun searchProductsRetrofit(searchTerm: String): List<Product> {
val options: MutableMap<String, String> = HashMap()
options["$filter"] = "contains(lower(Name),'$searchTerm')"
return retrofit().getProducts(options).value
}
在您的 API 界面中将其配置为使用传入的数据,如下所示:
@GET("/pmi/civic_amenity/CaProduct")
suspend fun getProducts(@QueryMap options: Map<String, String>): ProductsResponse