How to send an array to a modified android

How can I post the next parameter to a modification using the post method?

"params":{"body": { "learning_objective_uuids": [ "ED4FE2BB2008FDA9C8133FF462959C0968FAB98C4D1DB8F2" ], "note": "FasfAFSASFASDF", "user_uuids": [ "EDF8B7EC20005ACC5C40FF7D6E988801F5BAD83CBBCDB97F", "EDF8F78F2000569C64101F244AA20C0070D2A7FCB1939E19" ] } } } } 
+13
android retrofit
source share
6 answers
 @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 ); 

Best solution: use arraylist .. 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 ); 
+41
source share

see this example where I need to pass registration field data as a json request

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

here I created registrationData p>

 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; } 
+6
source share

Go to the website: JSON Schema 2 POJO

Paste your Json format and then

Select source type: JSON, annotation style: none

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

Then in your Api:

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

If you have headers, you can add them to such parameters:

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

@Edit: for your recall review my answer is: how-to-use-gson-2-0-on-onresponse-from-retrofit-2-0

+1
source share

if you want to send a list with the same name, the only thing to me @Query is using @Query

 @FormUrlEncoded @POST("service_name") void functionName( @Query("category") List<Int> categories ); 

it will be sent as follows: https://example.com/things?category=100&category=101&category=105

accepted answers do not work in Retrofit2

0
source share

Today launched implementation 'com.squareup.retrofit2:retrofit:2.1.0' Retrofit implementation 'com.squareup.retrofit2:retrofit:2.1.0'

It works great ...

 @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 ignore @Header and @Field("method") .... the main part is @Field("items[]") List<Integer> items

This is what allows you to send goods. On the API side, I'm just looking for an array of integers, and this works great.

0
source share

I found a new workaround:

You can send it as a string:

 @POST("CollectionPoints") @FormUrlEncoded Call<SomeResponse> postSomething(@Field("ids")String ids); 

and send it like this:

 Call<SomeResponse> call = service.postSomething("0","0", Arrays.toString(new int[]{53551, 53554})); 

Best wishes!

0
source share

All Articles