首页 > 解决方案 > 发送列表有问题上传文件到服务器改造时

问题描述

接口接口.java

public interface ApiInterface {
@Multipart
@POST("/register")
  registerUser(
@Part("first_name") RequestBody firstname,
@Part("last_name") RequestBody lastname, 
@Part("email") RequestBody email,
@Part MultipartBody.Part photo,
@Part("types") List<Integer> types);  ->This part I am     having    problem
}

Presenter.java

public void registerUser(String firstname, String lastname,   String emails, String passwords, File profilePhoto,   List<Integer> eventTagPks ) {
    MultipartBody.Part partProfilePhoto =    MultipartBody.Part.createFormData("profile_photo", profilePhoto.getName(), RequestBody.create(MediaType.parse("image/*"), profilePhoto));
    RequestBody name = RequestBody.create(MediaType.parse("multipart/form-data"),firstname);
    RequestBody surname = RequestBody.create(MediaType.parse("multipart/form-data"),lastname);
    RequestBody email = RequestBody.create(MediaType.parse("multipart/form-data"),emails);
    RequestBody birthdate = RequestBody.create(MediaType.parse("multipart/form-data"),birthday);
    RequestBody password = RequestBody.create(MediaType.parse("multipart/form-data"),passwords);
    //RequestBody eventTagPkList = RequestBody.create(MediaType.parse("multipart/form-data"), eventTagPks); ->I cannot create RequestBody that contains Integer List


    apiInterface.registerUser(name,surname,email,password,birthdate,partProfilePhoto,eventTagPks).enqueue(new CustomCallBack<RegisterResponse>(activity,null,registerView,CustomCallBack.Type.None) {
        @Override
        public void onResponse(@NonNull Call<RegisterResponse> call, @NonNull Response<RegisterResponse> response) {
            super.onResponse(call, response);
            if (response.isSuccessful()) {

            }
        }

        @Override
        public void onFailure(@NonNull Call<RegisterResponse> call, @NonNull Throwable t) {
            super.onFailure(call, t);
        }
    });
}

问题
我无法创建包含整数列表的 RequestBody

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

问题是这个只有 RequestBody 只接受 File、byte[]、String、ByteString 我如何发送带有@part 的 List 真正的问题是这个。谢谢你的帮助。

标签: androidretrofitretrofit2

解决方案


推荐阅读