How to Post Data in an Array in Android Using Retrofit 2

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



Leave a reply



Submit