使用改造2发布数组

问题描述 投票:0回答:1

enter image description here

我需要发布一组与会者,如上图所示。我尝试使用Hashmap,但是它给出了一些错误,我不知道这是服务器端错误还是翻新。这是我的代码:

API接口

    @FormUrlEncoded
    @POST("vendor/event/{id}/checkin")
    Call<DefaultResponse> updateAttendance(
            @Path("id") int id,
            @QueryMap Map<String,String> attendees,
            @Field("token") String token);

响应类别

String message;

    public DefaultResponse(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

这是我的活动代码

    final Map<String,String> attendees = new HashMap<>();
    for (int i = 0; i < attendeesTables.size(); i++){
            attendees.put("id", String.valueOf(attendeesTables.get(i).getId()));
            attendees.put("arrival_time", 
            attendeesTables.get(i).getArrival_time());
         }
    if (attendeesTables.size() > 0) {
            Call<DefaultResponse> call = RetrofitClient.getmInstance().getApi().updateAttendance(event_id,attendees,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();
            }
        });
      }

我是否以正确的方式做所有事情?我收到请求代码400(错误请求)

android post retrofit2
1个回答
1
投票

您的参数不正确,这就是为什么您会收到400错误。尝试如下]

请求:

@FormUrlEncoded
@POST("vendor/event/{id}/checkin")
Call<DefaultResponse> updateAttendance(
        @Path("id") int id,
        @Field("attendees") String attendees,
        @Field("token") String token);

Api呼叫:

    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();
            }
        });
    }
© www.soinside.com 2019 - 2024. All rights reserved.