How to post array in retrofit android
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.
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;
}
@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
);