android-kotlin JsonObject JsonArray通过POST发送请求数据

时间:2020-07-19 02:20:06

标签: android json kotlin post retrofit2

我想通过邮件发送数据请求到服务器,我想知道如何在数组中添加数据

lag()

请求

select count(distinct customer)
from (select s.*,
             lag(date) over (partition by customer order by date) as prev_date
      from sales s
     ) s
where prev_date > s.date - interval '12 month';

响应

data class City(
   @SerializedName("cityId")
   val cityId: Int?,
   @SerializedName("detail")
   val detail: List<String?>
)

API服务器

data class CityRequest(
   @SerializedName("listCity")
   val listCity: List<City?>
)

连接服务 我不知道如何向该部分添加信息。

data class CityResponse(
   @SerializedName("code")
   val code: String?,
   @SerializedName("status")
   val status: Boolean?,
   @SerializedName("message")
   val message: String?
)

JSON简单

@Headers("Content-Type: application/json")
@POST("city")
suspend fun sendCityContent(@Body listCity: CityRequest?): 
Call<CityResponse?>

接下来我该怎么办? 您可以为我提供阵列中添加数据的示例吗? 我想要的东西

private suspend fun sendDataCity(city: List<city?>) {
    val retrofit = clientCity
    val sendDataToServer = retrofit?.create(CityService::class.java)
    val call = sendDataToServer?.sendCityContent(CityRequest(city))
    call?.enqueue(object : Callback<CityResponse?> {
        override fun onResponse(
            call: Call<CityResponse?>, response: Response<CityResponse?>) {
            val getResponse = response.body()
            Timber.tag("SALE_CITY: ").d("code: %s", getResponse?.code)
            Timber.tag("SALE_CITY: ").d("status: %s", getResponse?.status)
            Timber.tag("SALE_CITY: ").d("message: %s", getResponse?.message)
        }

        override fun onFailure(call: Call<CityResponse?>, t: Throwable?) {
            t?.printStackTrace()
        }

    })
}

谢谢

1 个答案:

答案 0 :(得分:0)

我可以看到您的请求的一个问题是,密钥与您发送的密钥不同,可能与之不同。应该是city 而不是listCity

data class CityRequest(
   @SerializedName("city")
   val city: List<City?>
)

您的城市班级应该具有这些键answer,您已经将其称为details

data class City(
   @SerializedName("cityId")
   val cityId: Int?,
   @SerializedName("answer")
   val answer: List<String?>
)

我想您发送的密钥错误,这可能是服务器未接受请求的原因。进行上述更改,如果出现错误,则应在发布后起作用。