温馨提示:本文翻译自stackoverflow.com,查看原文请点击:android - How would I parameterise this endpoint using Retrofit please?
android kotlin retrofit

android - 请问如何使用Retrofit参数化此端点?

发布于 2020-03-31 23:11:42

假设我想查询一个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编写服务器

查看更多

提问者
Steve
被浏览
11
Steve 2020-02-03 19:11

答案是使用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