Kotlin Retrofit 查询注解参数
Kotlin Retrofit Query Annotation parameters
所以我有这个 NetworkConfig.kt
class 的界面:
interface getProductList {
@GET("stock")
fun getProducts(@Query("outcode") stkOutcode: String): Call<OutletListPOJODataClasses>
}
这是来自 Activity 的代码片段,我用来获取 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()
}
})
这是我使用的数据class:
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()
函数中传递什么?如果有任何不清楚的地方,请告诉我。
应该是
NetworkConfig().getProductListService()
.getProducts(stkOutcode = stkOutcodeValue)
....
其中 stkOutcodeValue
(String 类型)应该是已知的,或者可以使用默认值(如果适用)。
感谢@sonnet,本例中的端点是https://example.com/api/stock?outcode=stkOutcodeValue
所以我有这个 NetworkConfig.kt
class 的界面:
interface getProductList {
@GET("stock")
fun getProducts(@Query("outcode") stkOutcode: String): Call<OutletListPOJODataClasses>
}
这是来自 Activity 的代码片段,我用来获取 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()
}
})
这是我使用的数据class:
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()
函数中传递什么?如果有任何不清楚的地方,请告诉我。
应该是
NetworkConfig().getProductListService()
.getProducts(stkOutcode = stkOutcodeValue)
....
其中 stkOutcodeValue
(String 类型)应该是已知的,或者可以使用默认值(如果适用)。
感谢@sonnet,本例中的端点是https://example.com/api/stock?outcode=stkOutcodeValue