【问题标题】:How to send a list of image using multipart and retrofit如何使用多部分和改造发送图像列表
【发布时间】:2021-05-19 12:32:47
【问题描述】:

我必须上传一组图片,到目前为止我所做的是:

请求

@Multipart
    @POST("/user/api/v1/guest/readReceipts")
    suspend fun sendReceipt(
        @Body filesList: MutableList<MultipartBody.Part>
    ): Call<MutableList<OcrResponse>>

存储库

val requestFile = image.asRequestBody("multipart/form-data".toMediaTypeOrNull())
            val body = MultipartBody.Part.createFormData("file", "file.jpg", requestFile)
            ocrRequests.sendReceipt(mutableListOf(body))

但这不起作用。我也尝试将@Body 更改为@Part,但不起作用。

我现在知道这项工作

@Multipart
    @POST("profilo/avatar")
    fun setProfileImage(
        @Part file: MultipartBody.Part,
    ): Call<ProfileImageResponse>

因为我在其他项目中使用过它,但我无法编辑它以使用多部分列表

【问题讨论】:

  • “但这不起作用”——“不起作用”是什么意思?您的具体症状是什么?

标签: android retrofit multipart


【解决方案1】:

照片列表 arg 应使用@Part 注释标记

@Multipart
@POST("/user/api/v1/guest/readReceipts")
suspend fun sendReceipt(
    @Part filesList: MutableList<MultipartBody.Part>
): Call<MutableList<OcrResponse>>

在数组表单数据的情况下,应该有方括号[] 作为MultipartBody.Part 字段名称的后缀

MultipartBody.Part.createFormData("file[]", "file.jpg", requestFile)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-15
    • 2018-04-15
    • 2017-01-14
    • 2015-11-29
    • 1970-01-01
    • 2015-09-16
    相关资源
    最近更新 更多