Post an array using retrofit 2
You params are not correct that's why you getting 400 error. try like below
Request :
@FormUrlEncoded
@POST("vendor/event/{id}/checkin")
Call<DefaultResponse> updateAttendance(
@Path("id") int id,
@Field("attendees") String attendees,
@Field("token") String token);
Api Call :
JSONArray attendeesArray=new JSONArray();
for (int i = 0; i < attendeesTables.size(); i++){
JSONObject jsonObject=new JSONObject();
jsonObject.put("id",String.valueOf(attendeesTables.get(i).getId()));
jsonObject.put("arrival_time",String.valueOf(attendeesTables.get(i).getArrival_time()));
attendeesArray.put(jsonObject);
}
if (attendeesTables.size() > 0) {
Call<DefaultResponse> call = RetrofitClient.getmInstance().getApi().updateAttendance(event_id,attendeesArray.toString(),token);
call.enqueue(new Callback<DefaultResponse>() {
@Override
public void onResponse(Call<DefaultResponse> call, Response<DefaultResponse> response) {
Toast.makeText(EventsDetailsActivity.this, response.code()+"", Toast.LENGTH_SHORT).show();
Toast.makeText(EventsDetailsActivity.this, response.isSuccessful()+"", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<DefaultResponse> call, Throwable t) {
Toast.makeText(EventsDetailsActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
Android Retrofit 2 - problem with sending the Array<Object> using POST
Post assortment in my problem will be in a list, so to make it works I need to change the Call to
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
);
POST request of an array into JSON with Retrofit 2 Android
I can't understand your question well.
I can explain how to send data into node JS using retrofit2
In android, add this line in the Retrofit interface
@POST("/user/phone/verify")
Call<Void> addNewComment(@Body HashMap<String, String> map);
And add your details about comment here :
HashMap<String,String> map = new HashMap<>();
map.put("id","your id");
map.put("image","your image");
map.put("comment","your comment");
map.put("password",password);
Then this code call the post method as the above HashMap as body
Call<Void> call = retrofitInterface.executeSignUp(map);
call.enqueue(new Callback<Void>() {
@Override
public void onResponse(Call<Void> call, Response<Void> response) {
if (response.code()==200){
//Success
}else {
//An error ocuurred
}
}
@Override
public void onFailure(Call<Void> call, Throwable t) {
Log.e("ServerCallback", "onFailure: ",t);
}
});
Retrofit How to post data on specific field only?
As per screenshot. body is not required. So it can be like this
@POST("https://www.mmsnusindo.com/api/ver/kunjungan/rejected")
Call<List<Verify>> verifikasiKunjunganRejected(@Header("api-key") String content_type,
@Query("id") String id;// please check your id is int or String.
Related Topics
Android Room Persistent: Appdatabase_Impl Does Not Exist
How to Efficiently Read Multiple Json Files into a Dataframe or Javardd
Name a File in Java to Include Date and Time Stamp
Placing a Jlabel At a Specific X,Y Coordinate on a Jpanel
In Junit 5, How to Run Code Before All Tests
Type Safety: Unchecked Cast from Object to Arraylist<Myvariable>
How to Get Access to Job Parameters from Itemreader, in Spring Batch
How to Get Amount of Tens in a Int
Cannot Load Driver Class: Com.Mysql.Jdbc.Driver Spring Boot
Why How to Declare a Variable Without an Initial Value
Java - Reading Text File as an Integer and Split Each Line into Array
Jackson Serializes a Zoneddatetime Wrongly in Spring Boot
Delete a Line from a File Using Java
Java Lombok: Omitting One Field in @Allargsconstructor
How to Check If an Arraylist of Strings Contains Substrings of Another Arraylist of Strings
Generating Unique Random Numbers Effectively in Java
How to Call an Excel Vba Macro from Java Code
Cannot Get Jedis Connection; Could Not Get a Resource from the Pool