Kotlin改装查询注释参数 [英] Kotlin Retrofit Query Annotation parameters
问题描述
所以我有一个NetworkConfig.kt
类的接口:
So I have have this an interface for the NetworkConfig.kt
class:
interface getProductList {
@GET("stock")
fun getProducts(@Query("outcode") stkOutcode: String): Call<OutletListPOJODataClasses>
}
这是来自 Activity 的代码段,我用来获取url:
and this is a code snippet from the Activity I use to fetch url:
NetworkConfig().getProductListService()
.getProducts() //What should i pass here ?
.enqueue(object : Callback<ProductListPOJODataClasses> {
override fun onFailure(call: Call<ProductListPOJODataClasses>, t: Throwable) {
Toast.makeText((activity as AppCompatActivity), t.localizedMessage, Toast.LENGTH_SHORT).show()
}
override fun onResponse(
call: Call<ProductListPOJODataClasses>,
response: Response<ProductListPOJODataClasses>
) {
binding.rvProductList.adapter = response.body()?.let { ProductListAdapter(it, this@ProductListFragment) }
Toast.makeText((activity as AppCompatActivity), "Data retrieved!", Toast.LENGTH_SHORT).show()
}
})
这是我使用的数据类:
data class ProductListPOJODataClassesDataItem(
@field:SerializedName("stk_prodcode")
val stkProdcode: String? = null,
@field:SerializedName("stk_allqty")
val stkAllqty: Int? = null,
@field:SerializedName("pro_saleprice")
val proSaleprice: Int? = null,
@field:SerializedName("skt_lastupdate")
val sktLastupdate: String? = null,
@field:SerializedName("stk_outcode")
val stkOutcode: String? = null,
@field:SerializedName("pro_name")
val proName: String? = null
)
我在使用这个库方面还很陌生.我想知道的是,我应该在上面的.getProducts()
函数中传递什么?如果有任何不清楚的地方,请通知我.
I'm quite new in using this library. What I want to know is what should I pass in the .getProducts()
function above? If there's anything unclear, let me know.
推荐答案
应该是
NetworkConfig().getProductListService()
.getProducts(stkOutcode = stkOutcodeValue)
....
其中stkOutcodeValue
( String 类型)应该是已知的,或者可以使用默认值(如果适用).
where stkOutcodeValue
(String type) should be known or a default value might be used, if applicable.
感谢@sonnet,本例中的终结点为https://example.com/api/stock?outcode=stkOutcodeValue
Thanks to @sonnet, the endpoint in this case is https://example.com/api/stock?outcode=stkOutcodeValue
这篇关于Kotlin改装查询注释参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!