0

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?

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
  • 1
    Does this answer your question? [How to POST raw whole JSON in the body of a Retrofit request?](https://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request) – Jitesh Prajapati Jul 29 '22 at 09:12
  • Retrofit tends to reconstruct your Json. It is best to create an object and send the object as the body. – Emmanuel Conradie Jul 29 '22 at 09:16
  • @JiteshPrajapati i already tried that but it didn't worked. But converting to object works for me. Thanks for your reply. – George Glyn Jul 29 '22 at 10:47

1 Answers1

1

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)
Aymen Ben Salah
  • 489
  • 4
  • 13