簡體   English   中英

kotlin retrofit 我怎樣才能從這個 Z572D4E421E5E6B9BC121D815E8A0271 得到 json?

[英]kotlin retrofit how can i get json from this url?

我有一個 URL,我想用 retrofit 請求Json ,但我不知道該怎么做有人可以幫我嗎?

這是我的基礎url

https://api.basalam.com/api/user

Request Body (JSON - POST):
{"query": "{productSearch(size: 20) {products {id name
photo(size: LARGE) { url } vendor { name } weight price
rating { rating count: signals } } } }"

您可以查看本教程並了解更多信息:

首先你需要 model(數據類)

data class CustomizeWrapper(
    @SerializedName("success")
    var success: Boolean,
    @SerializedName("message")
    var message: String,
    @SerializedName("data")
    var data: Int
)

第二件事你需要 End pint 接口,它將包含你所有的端點

interface EndpointInterface {

    @FormUrlEncoded
    @POST("add_customize")
    fun addCustomize(
        @Field("customize_id") customize_id: Int,
        @Field("item_id") item_id: Int,
        @Field("price") price: Float,
        @Field("quantity") quantity: Int,
        @Field("company") company: String
    ): Call<CustomizeWrapper>

}

最后一件事你需要調用 API

private fun addCustomize(customizeId: Int, item_id: Int, price: Float) {

    val call: Call<CustomizeWrapper> =  App.instance.api.getData.addCustomize(customizeId, item_id, price, 1, App.instance.cacheManager.getCompanyName() )
    call.enqueue(object : Callback<CustomizeWrapper> {

        override fun onResponse(call: Call<CustomizeWrapper>?, response: retrofit2.Response<CustomizeWrapper>?) {
            AppLogger.log("test", response.toString())
            AppLogger.log("test", response!!.body().toString())

        }

        override fun onFailure(call: Call<CustomizeWrapper>?,  t: Throwable?) {
            AppLogger.log("test", "Failed to add order : $t")

        }
    })
}

但不要忘記檢查您的 API 是否為(POST、GET、....)

您必須在 EndpointInterface 的 function 中定義它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM