kotlin改型如何从这个url获取json?

nukf8bse  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(308)

我有一个网址,我想请求 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 } } } }"
kuuvgm7e

kuuvgm7e1#

您可以查看本教程,了解更多信息:
首先需要模型(数据类)

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的函数中定义它

相关问题