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
Cart Item Count Increment/Decrement & Add to Cart
How to Resolve Javax.Xml.Bind.Jaxbcontext in Eclipse
How to Make a Textview Automatically Scroll as I Add More Lines of Text
Android Set Height and Width of Custom View Programmatically
Check If an Email Exists Before Creating a User Firebase Android
Play Store Alpha Test Download Link Not Working
How to Change the Icon Color Selected on Bottom Navigation Bar in Android Studio
How to Disable Preveious Date in a Date Picker
Cannot Keep Android Service Alive After App Is Closed
Android Sdk Is Mssing ,Out of Date or Corrupted
Open Play Store App from Browser Link
Method to Refresh Fragment Content When Data Changed ( Like Recall Oncreateview)
How to Set Spinner Default by Its Value Instead of Position
How to Display a Pdf Via Android Web Browser Without "Downloading" First
Detect or Prevent If User Uses Fake Location
How to Post Raw Whole Json in the Body of a Retrofit Request