我需要用kotlin改造把图像发送到服务器。我尝试使用multipart,但是图像为null,请求是奇怪的!
我如何使用多部分与kotlin和改造,以发送图像到服务器?
谢谢
发布于 2022-06-30 08:36:00
在您的APIInterface函数上添加如下多部分标记
@Multipart
@POST(your api url)
suspend fun uploadImage(
@Header("Header") header: String?,
@Part imageFile: MultipartBody.Part
): Response<StatusResponse>然后像这样从你的活动中调用这个函数
val imageFile = File(yourImage)
val requestFile: RequestBody = RequestBody.create(
"image/jpg".toMediaTypeOrNull(),
imageFile
)
val image =
MultipartBody.Part.createFormData("picture", imageFile.name, requestFile)
apiClient.uploadImage(header,image)https://stackoverflow.com/questions/72812263
复制相似问题