为什么要与“图像”/*&quot倍增改造。得到不好的要求?
我想发布一个带有一些细节的产品,例如名称,desc等。以及图像。但是我得到了400个不好的要求。
val namebody = name.toRequestBody("text/plain".toMediaType())
val priceBody = price.toRequestBody("text/plain".toMediaType())
val descriptionBody = description.toRequestBody("text/plain".toMediaType())
val categoryBody = category.toRequestBody("text/plain".toMediaType())
val locationBody = location.toRequestBody("text/plain".toMediaType())
val imageBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
val image = MultipartBody.Part.createFormData("image", imageFile.name, imageBody)
@Multipart
@POST("seller/product")
suspend fun postProductSeller(
@Header("access_token") access_token: String,
@Part("name") name: RequestBody,
@Part("description") description: RequestBody,
@Part("base_price") base_price: RequestBody,
@Part("category_ids") category_ids: RequestBody,
@Part("location") location: RequestBody,
@Part image: MultipartBody.Part
)
因此,经过多次尝试,我很成功地将图像进行改造,但我必须将此行更改
val imageBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
为Spesific Image格式,示例JPG,PNG。
val imageBody = imageFile.asRequestBody("image/jpg".toMediaTypeOrNull())
有人知道为什么会发生这种情况吗?如果要上传任何形式的图像文件,该怎么办?谢谢
I want to post a Product with some details like name, desc, etc. and image too. But I got 400 bad request.
val namebody = name.toRequestBody("text/plain".toMediaType())
val priceBody = price.toRequestBody("text/plain".toMediaType())
val descriptionBody = description.toRequestBody("text/plain".toMediaType())
val categoryBody = category.toRequestBody("text/plain".toMediaType())
val locationBody = location.toRequestBody("text/plain".toMediaType())
val imageBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
val image = MultipartBody.Part.createFormData("image", imageFile.name, imageBody)
@Multipart
@POST("seller/product")
suspend fun postProductSeller(
@Header("access_token") access_token: String,
@Part("name") name: RequestBody,
@Part("description") description: RequestBody,
@Part("base_price") base_price: RequestBody,
@Part("category_ids") category_ids: RequestBody,
@Part("location") location: RequestBody,
@Part image: MultipartBody.Part
)
So after many tries, I have success to post image with retrofit but I have to change this line
val imageBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
to a spesific image format, example jpg, png.
val imageBody = imageFile.asRequestBody("image/jpg".toMediaTypeOrNull())
And anyone knows why is this happen? And how should I do if I want to upload any kind of image files? Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论