Kotlin Retrofit Query Annotation parameters

和自甴很熟 提交于 2021-02-10 15:10:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!