Okhttp:如何使用循环添加多部分表单部分?

Okhttp: How to add multi-part form part using a loop?

我正在使用 OKHttp 发送多部分数据。这是我的代码:

 MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
bodyBuilder.setType(MultipartBody.FORM)
                    .addFormDataPart("arrival_to", object.optString("arrival_to"))
                    .addFormDataPart("centre_id", object.optString("centre_id"))
                    .addFormDataPart("departure_from", object.optString("departure_from"))
                    .addFormDataPart("expense_item_id", object.optString("expense_item_id"))
                    .addFormDataPart("expense_sub_item_id", object.getString("expense_sub_item_id"))
                    .addFormDataPart("file_list", object.optString("file_list"))
                    .addFormDataPart("journey_date", object.optString("journey_date"))
                    .addFormDataPart("passenger_age", object.optString("passenger_age"))
                    .addFormDataPart("passenger_gender", object.optString("passenger_gender"))
                    .addFormDataPart("passenger_name", object.optString("passenger_name"))
                    .addFormDataPart("personal_id", object.optString("personal_id"))
                    .addFormDataPart("remarks", object.optString("remarks"))
                    .addFormDataPart("request_amount", object.optString("request_amount"))
                    .addFormDataPart("request_status", object.optString("request_status"))
                    .addFormDataPart("travel_class", object.optString("travel_class"))
                    .addFormDataPart("vehicle_name_no", object.optString("vehicle_name_no"))
                    .addFormDataPart("vendor_id", object.optString("vendor_id"))
                    .addFormDataPart("vendor_type_id", object.optString("vendor_type_id"))
                    .addFormDataPart("within_budget", object.optString("within_budget"))
                    .addFormDataPart("file_list", attach_file, RequestBody.create(MediaType.parse(type), file))
                    .build();

这里的问题是我正在一个接一个地添加表单数据部分,这在这里没问题,但我想使用循环来添加表单数据部分以防数据集增加。此外,我正在制作一个单独的 class 包含上传数据的代码,如何将键值对传递给 class 我应该使用地图吗?

也许这样的方法可行?

MultipartBody.Builder bodyBuilder = new MultipartBody.Builder();
MultipartBody.Builder builder = bodyBuilder.setType(MultipartBody.FORM);

String[] keys = new String[]{
        "arrival_to",
        "centre_id",
        "departure_from",
        "expense_item_id",
        "...."

};

Arrays.stream(keys)
        .forEach(key -> {
            builder.addFormDataPart(key, object.optString(key));
        });

}