Search code examples
androidretrofit2

How to send JSON as Retrofit POST request


I'm trying to send a POST request in Retrofit for below JSON.

{"data":["dog playing football",45,256,256,2,5]} 

I have sent the same request using Volley. Now I'm switching to Retrofit.

    val data = JSONObject()
    val arr = JSONArray()
    arr.put(inputText)
    arr.put(steps)
    arr.put(width)
    arr.put(height)
    arr.put(numberOfImages)
    arr.put(diversityScale)
    data.put("data", arr)

How can I send the same using Retrofit?


Solution

  • In your case you can use like this :

       val data = JSONObject()
        val arr = JSONArray()
        arr.put(inputText)
        arr.put(steps)
        arr.put(width)
        arr.put(height)
        arr.put(numberOfImages)
        arr.put(diversityScale)
        data.put("data", arr)
    
    val map = ObjectMapper().readValue<MutableMap<Any, Any>>(data.toString())
    
    
    @POST("your_url_here")
    Call<Object> yourFunName(@Body Map<String, String> body)