android - 改造多部分数组键,但仅限单个值
问题描述
我的目标或目标是发出这样的请求,以便它成功:
现在我当前的代码如下所示:
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 并用它设置一个值?
我只想复制上面的图像,以便我可以正确解决这个值。
解决方案
您的请求应该是这样的:
@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();
}
}
推荐阅读
- javascript - 如何返回包含值的索引列表
- javascript - 滚动到输入字段但偏移固定导航?
- mongodb - 恢复 mongodump 时的性能问题。我能做些什么更好?
- google-apps-script - 用于在 Google 工作表中选择行并清除内容的 Google 脚本
- java - Grails 3.2 文件上传错误 (FileNotFoundException )
- node.js - 承诺然后在发布请求完成之前阻止执行?
- wordpress - WP_查询。按标签获取帖子
- javascript - 服务器上的文件更改了内容,但 js 仍然获取旧数据
- c# - 从字典中检索值的问题
- python-3.x - hHw 循环一个数字以交替填充列表