How to post array in retrofit android

前端 未结 7 1755
旧巷少年郎
旧巷少年郎 2020-11-27 19:55

How can I post following parameter in retrofit through post method ?

 \"params\":{\"body\": {
    \"learning_objective_uuids\": [
      \"ED4FE2BB2008FDA9C81         


        
相关标签:
7条回答
  • 2020-11-27 20:26

    Gson is the Best solution for JSON Object/Array related problems.

    Here, I am sharing my easiest solution for passing array type value in retrofit API

    id: ArrayList<String> //Already initilized 
    status: String  //Already initilized 
    
    val jsonObject = JsonObject()
    val toJson = Gson().toJsonTree(id) //Only one line to covert array JsonElement
    
    jsonObject.add("id", toJson) //Add Json Element in JsonObject
    
    jsonObject.addProperty("status", status)
    

    API Calling using jsonObject

    @POST("API_END_POINT")
    fun changeStatusOfList(@Body jsonObject: JsonObject): Observable<Response<RETURN_TYPE>>
    

    Output in Log:

    {"id":["426","427"],"status":"1"}
    
    0 讨论(0)
提交回复
热议问题