0

This is the interface method:

@FormUrlEncoded
@POST (“url/myurl")
Call<JsonArray> sendParameters (@Header("x-imei") String imei, @Header("x-id-cliente") String idCliente, @Field(“param1") JsonObject param1, @Field(“param2") JsonArray param2, @Field(“param3") JsonArray param3, @Field(“param4") JsonArray param4,@Field(“param5") JsonArray param5);

And the method to use it:

Call<JsonArray> peticion= RetrofitClient.getRetrofitClient(getActivity()).sendParameters(settings.getString("imei", ""), settings.getString("idCliente", "”),param1,param2,param3,param4,param5);

This way, the call is not working.

I tried changing all the interface parameters to String, and doing param1.toString(), param2.toString() and so on in the call, and is not working neither.

Is there a simple way to send JsonObjects and JsonArrays in a POST, using Retrofit 2?

Thank you.

Fustigador
  • 6,339
  • 12
  • 59
  • 115

1 Answers1

0

First you can create a custom class which you want to pass as a POST body

class CustomClass {
   String param1;
   String parma2;
   String param3;
   //all of your params with getters and setters
}

Then you change the Retrofit method according to your new CustomClass instance with the @Body annotation:

@POST (“url/myurl")
Call<JsonArray> sendParameters (@Header("x-imei") String imei, @Header("x-id-cliente") String idCliente, @Body CustomClass customClassInstance);

And eventually you make the call to post data:

CustomClass customClassInstance = new CustomClass();
customClassInstance.setParam1(...);
//...and so on

Call<JsonArray> peticion= RetrofitClient.getRetrofitClient(getActivity()).sendParameters(settings.getString("imei", ""), settings.getString("idCliente", ""), customClassInstance);

EDIT: removed the @FormUrlEncoded annotation

Niccolò Passolunghi
  • 5,966
  • 2
  • 25
  • 34