android - 请问我将如何使用 Retrofit 参数化这个端点?
问题描述
假设我想查询一个 API,该 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
推荐阅读
- python - 使用根记录器禁用来自导入模块的日志
- python - 'Line 2D' 对象没有属性 'kind' -- pyplot.plot() 和 .plot() 不同吗?
- android - 用谷歌地图规划路线
- c++ - 在数字右侧使用左移运算符
- java - 使用 Gradle 将目录压缩到多个目的地
- api - 如何将 API 输出数据保存到 Azure 数据工厂中的数据集
- c - 我在 scanf 中的输入不起作用,返回进程并且我没有得到任何输出
- admob - Admob 上的应用打开广告 - 测试广告上没有广告配置
- python - 对数组的每个元素执行相同操作的最快方法
- ios - 自定义 XCFramework 的消费者如何访问标头文档?