How can I post following parameter in retrofit through post method ?
\"params\":{\"body\": {
\"learning_objective_uuids\": [
\"ED4FE2BB2008FDA9C81
if you want to send a list of the same name the only thing that worked for me in retrofit2 is to use @Query
@FormUrlEncoded
@POST("service_name")
void functionName(
@Query("category") List<Int> categories
);
this will send it like: https://example.com/things?category=100&category=101&category=105
the accepted answers seem not to work in Retrofit2
I've found a new workaround:
you can send it as a String:
@POST("CollectionPoints")
@FormUrlEncoded
Call<SomeResponse> postSomething(@Field("ids")String ids);
and send pass it like this:
Call<SomeResponse> call = service.postSomething("0","0", Arrays.toString(new int[]{53551, 53554}));
Best Regards!
@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
);
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", "abc@xyz.com");
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;
}
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
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.