Android retrofit2 post上传图片的同时 要上传多个参数

//构建body
        //addFormDataPart()第一个参数为表单名字,这是和后台约定好的
        Model model = new Model(mContext);
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("iD", model.getID())
                .addFormDataPart("version", model.getVersion())
                .addFormDataPart("token", model.getToken())
                .addFormDataPart("s", model.getS());
        //注意,file是后台约定的参数,如果是多图,file[],如果是单张图片,file就行
        //这里上传的是多图
        builder.addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));

        RequestBody requestBody = builder.build();

        

然后把requestBody传过去就行了

你可能感兴趣的:(Android进阶)