How to Post Array in Retrofit Android

How to post array in retrofit android

@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
);

how to send json array in android retrofit?

You can directly send the array of objects as parameter. Retrofit will handle the conversion. Change your interface method like this:

@POST("example/api/order")
Call<JSONArray> postOrder(@Body List<Cart> cartList);

Check this link, you will get an idea.

How to send Arrays / Lists with Retrofit

To send as an Object

This is your ISearchProfilePost.class

@FormUrlEncoded
@POST("/profile/searchProfile")
Call<ResponseBody> postSearchProfile(@Body ArrayListAge ages);

Here you will enter the post data in pojo class

public class ArrayListAge{
@SerializedName("age")
@Expose
private ArrayList<String> ages;
public ArrayListAge(ArrayList<String> ages) {
this.ages=ages;
}
}

Your retrofit call class

ArrayList<Integer> ages = new ArrayList<>();
ages.add(20);
ages.add(30);

ArrayListAge arrayListAge = new ArrayListAge(ages);
ISearchProfilePost iSearchProfile = gsonServerAPIRetrofit.create(ISearchProfilePost.class);
Call<ResponseBody> call = iSearchProfile.postSearchProfile(arrayListAge);

To send as an Array List check this link https://github.com/square/retrofit/issues/1064

You forget to add age[]

@FormUrlEncoded
@POST("/profile/searchProfile")
Call<ResponseBody> postSearchProfile(
@Field("age[]") List<Integer> age
};

Sending array value as parameter retrofit android

 ArrayList<String> countriedAplied = new ArrayList<>();

JSONArray jsonArray = new JSONArray();
JSONObject jsonObject=new JSONObject();

jsonArray.put(countriedAplied);
try {
jsonObject.put("Country",jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}

JsonParser jsonParser = new JsonParser();
JsonObject gsonObject = new JsonObject();
gsonObject = (JsonObject) jsonParser.parse(jsonObject.toString());
sendtoDB(gsonObject);


Related Topics



Leave a reply



Submit