George Glyn
George Glyn

Reputation: 25

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?

Upvotes: 0

Views: 473

Answers (1)

Aymen Ben Salah
Aymen Ben Salah

Reputation: 509

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)

Upvotes: 1

Related Questions