问题
So I have have this an interface for the NetworkConfig.kt
class:
interface getProductList {
@GET("stock")
fun getProducts(@Query("outcode") stkOutcode: String): Call<OutletListPOJODataClasses>
}
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()
}
})
And this is the data class I use:
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
)
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.
回答1:
It should be
NetworkConfig().getProductListService()
.getProducts(stkOutcode = stkOutcodeValue)
....
where stkOutcodeValue
(String type) should be known or a default value might be used, if applicable.
Thanks to @sonnet, the endpoint in this case is https://example.com/api/stock?outcode=stkOutcodeValue
来源:https://stackoverflow.com/questions/62086824/kotlin-retrofit-query-annotation-parameters