Retrofit多部分数组键,但仅单个值

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

我的目标或目标是发出这样的请求,以便成功:

This is my goal

现在我当前的代码如下:

WebApi client = ServiceGenerator.createService(WebApi.class);

    final Call<BaseResponse<BookingInfoEntity>> call = client.startBooking(

     WebUtilities.createPartFromString(SharePreferences.getUserId(context)), 
     WebUtilities.createPartFromString(""),  //trainer_id
     WebUtilities.createPartFromString(String.valueOf(listener.getTrainerTypesObject().get(0))), //train type
     WebUtilities.createPartFromString(String.valueOf(hours)), 
     WebUtilities.createPartFromString(String.valueOf(payment_id)), 
     WebUtilities.createPartFromString(SharePreferences.getCityId(context)),
     WebUtilities.createPartFromString(listener.getSelectedDate())); 
}


public static RequestBody createPartFromString(String descriptionString) {
    return RequestBody.create(okhttp3.MultipartBody.FORM, descriptionString);
}

@Multipart
@POST(START_BOOKING)
    Call<BaseResponse<BookingInfoEntity>> startBooking(@Part("trainee_id") RequestBody trainee_id,
                                                       @Part("trainer_id") RequestBody trainer_id,
                                                       @Part("trainer_types[][trainer_type_id]")RequestBody train_type,
                                                       @Part("number_of_hours") RequestBody number_of_hours,
                                                       @Part("payment_type_id") RequestBody payment_type_id,
                                                       @Part("city_id") RequestBody city_id,
                                                       @Part("meeting_date") RequestBody meeting_date);

我的问题是:我如何动态插入

trainer_types [] [trainer_type_id],

就像我想要5个教练类型ID时一样,如何在他们的键中插入5 trainer_type_id并为其设置值?

我只想复制上面的图像,以便我可以正确地解决此值。

android retrofit postman multipartform-data multipart
1个回答
0
投票

您的要求应该是这样:

@Multipart
@POST(START_BOOKING)
    Call<BaseResponse<BookingInfoEntity>> startBooking(@Part("trainee_id") RequestBody trainee_id,
                                                       @Part("trainer_id") RequestBody trainer_id,
                                                       @Part List<MultipartBody.Part> train_type,
                                                       @Part("number_of_hours") RequestBody number_of_hours,
                                                       @Part("payment_type_id") RequestBody payment_type_id,
                                                       @Part("city_id") RequestBody city_id,
                                                       @Part("meeting_date") RequestBody meeting_date);

创建文件类型的trainer_types列表

@NonNull
private MultipartBody.Part prepareFilePart(String partName, Uri fileUri, String file_type) {

    File file;
    if (file_type.contains("video")) {
        file = new File(fileUri.getPath());
    } else {
        file = FileUtils.getFile(this, fileUri);
    }

    RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);

    // MultipartBody.Part is used to send also the actual file name
    return MultipartBody.Part.createFormData(partName, file.getName().replaceAll(" ", "%20"), requestFile);
}

使用多个相同键维数组调用API

   private void startUploadingList(ArrayList<String> selectedItems1) {

        List<MultipartBody.Part> parts = new ArrayList<>();
        try {


            for (int i = 0; i < selectedItems1.size(); i++) {
                if (selectedItems1.get(i) != null) {

                        parts.add(prepareFilePart("trainer_types[][" + i + "]", Uri.parse(selectedItems1.get(i)), "image/png"));

                }
            }


            RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), "Your Value");

            // add another part within the multipart request
            // finally, execute the request
            Call<BaseResponse<BookingInfoEntity>> call = APIClient.getInterface().startBooking(description ,description ,parts, description,description ,description ,description );

            call.enqueue(new Callback<BaseResponse<BookingInfoEntity>>() {
                @Override
                public void onResponse(Call<BaseResponse<BookingInfoEntity>> call, Response<BaseResponse<BookingInfoEntity>> response) {
Log.e("Upload", "success");

                }

                @Override
                public void onFailure(Call<BaseResponse<BookingInfoEntity>> call, Throwable t) {
                    call.cancel();
                    Log.e("Upload error:", t.getMessage());
                    t.printStackTrace();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }           
© www.soinside.com 2019 - 2024. All rights reserved.