How to post array in retrofit android

How can I post following parameter in retrofit through post method ?

 "params":{"body": {
    "learning_objective_uuids": [
      "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2"
    ],
    "note": "FasfAFSASFASDF",
    "user_uuids": [
      "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F",
      "EDF8F78F2000569C64101F244AA20C0070D2A7FCB1939E19"
    ]
  }
}
} }

Solution 1:

@FormUrlEncoded
@POST("service_name") 
   void functionName(
        @FieldMap Map<String, String> learning_objective_uuids, @FieldMap Map<String, String> user_uuids, @Field("note") String note,
        Callback<CallBackClass> callback
    );

Better solution : Use arraylist.. Reference link : johnsonsu

@FormUrlEncoded
    @POST("service_name") 
       void functionName(
            @Field("learning_objective_uuids[]") ArrayList<String> learning_objective_uuids, @Field("user_uuids[]") ArrayList<String> user_uuids, @Field("note") String note,
            Callback<CallBackClass> callback
        );

Solution 2:

see this example where i need to pass registration fields data as json request

@POST("magento2apidemo/rest/V1/customers")
Call<RegisterEntity> customerRegistration(@Body JsonObject registrationData);

here i have created registrationData is

private static JsonObject generateRegistrationRequest() {
        JSONObject jsonObject = new JSONObject();
        try {
            JSONObject subJsonObject = new JSONObject();
            subJsonObject.put("email", "[email protected]");
            subJsonObject.put("firstname", "abc");
            subJsonObject.put("lastname", "xyz");

            jsonObject.put("customer", subJsonObject);
            jsonObject.put("password", "password");

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonParser jsonParser = new JsonParser();
        JsonObject gsonObject = (JsonObject) jsonParser.parse(jsonObject.toString());
        return gsonObject;
    }

Solution 3:

As of today, running the Retrofit implementation 'com.squareup.retrofit2:retrofit:2.1.0'

This works perfectly...

@FormUrlEncoded
@POST("index.php?action=item")
Call<Reply> updateManyItem(@Header("Authorization") String auth_token, @Field("items[]") List<Integer> items, @Field("method") String method);

You can disregard the @Header and @Field("method") .... the main piece is @Field("items[]") List<Integer> items

This is what allows you to send the items. On the API side I am simply looking for an array of integers and this works perfectly.

Solution 4:

Go to this site : JSON Schema 2 POJO

Paste your example Json format and then

Select source type : JSON , annotation style : None

Create a POJO class then , for example your class name : MyPOJOClass

Then in your Api :

@POST("endpoint")
public Call<Void> postArray(@Body MyPOJOClass mypojoclass);

If you have headers too you can add them in parameters like that :

@Header("Accept") String accept,@Header("Content-Type") String contentType

@Edit : for your comment checkout my answer : how-to-use-gson-2-0-on-onresponse-from-retrofit-2-0