Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] retrofit2 – How to include “param” name in JSON @Body Retrofit Android

Posted on November 14, 2022

Solution 1 :

Construct your body as an object of a data class as below

data class APIRequestPayload (
    val parametros: List<APIParameter>
)

data class APIParameter (
    val Nombre: String,
    val Valor: String
)

then use it as the below on service

fun getMovimientos(@Body parametros: APIRequestPayload): Call<BalanceVO>

To use it, pass the body as an obj,

service.getSaldo(parametros=APIRequestPayload(parametros=List<APIParameter>)).enqueue()

Still naming the val Nombre is not conventional, you may annotate with @SerializedName("Nombre") val nombre to stick with variable naming conventions if using GSON

Solution 2 :

Create a data class like this and assign list to it and pass it as body

data class ParametrosBody(val parametros: List<APIParameter>)
val parametros = ...//your list
val body = ParametrosBody(parametros)
interface APIService {

    @POST("/getMovimientos")
    fun getSaldo(@Body parametros: ParametrosBody): Call<BalanceVO>
}

Problem :

I need to call a service from Android.
I tested the service with Postman and it works, but the services receives one parameter named “parametros”, is a ArrayList in JSON format, it looks like this
enter image description here

the right format is

{
    "parametros": [
        {
            "Nombre": "@@IdCliente",
            "Valor": "1"
        }
    ]
}

it’s with the param name “parametros” included.
In postman it works fine.

But in Android I have:

interface APIService {

    @POST("/getMovimientos")
    fun getMovimientos(@Body parametros: List<APIParameter>): Call<BalanceVO>
}

and my request looks like this:

D/OkHttp: --> POST https://xxxxxxx/getMovimientos
    Content-Type: application/json; charset=UTF-8
    Content-Length: 38
D/OkHttp: [{"Nombre":"@@IdCliente","Valor":"1"}]
    --> END POST (38-byte body)

The parameter

[{"Nombre":"@@IdCliente","Valor":"1"}]

should be

"parametros":[{"Nombre":"@@IdCliente","Valor":"1"}]

How can I configure my app to put this param name?

Comments

Comment posted by RaZoDiuM

Thank you very much !!!, it solves my problem, I just need encapsulate my

READ  [ANSWERED] java - Android Studio - get data from SQLite after thread done
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress