我尝试通过翻新/分段上传图片(jpg)。 Api始终返回带有“无效查询参数:无效%-encoding”的代码400 我不知道怎么了。
我的界面:
@Headers("Content-type: application/x-www-form-urlencoded", "Accept: application/vnd.mypb+json; version=7")
@POST("/alerts/actions/unknown_pid")
@Multipart
fun uploadPhoto(@Part body: MultipartBody.Part): Call<String?>
我的网络服务上传照片方法:
fun uploadPhoto(bitmap: Bitmap) {
val imageFile = File(context.cacheDir, "image.jpg")
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 15, baos)
val byteArray = baos.toByteArray()
val fos = FileOutputStream(imageFile)
fos.write(byteArray)
fos.flush()
fos.close()
val filePart = RequestBody.create(MediaType.parse("image/*"), imageFile)
val body = MultipartBody.Part.createFormData("document[file]", "photo.jpg", filePart)
service.uploadPhoto(body).enqueue(object : LoggingCallback<String?>() {
override fun onSuccess(responseBody: String?) {
EventBus.getDefault().post(OnPhotoUploadSuccessfulEvent())
}
override fun onFailure(response: Response<String?>) {
EventBus.getDefault().post(OnPhotoUploadFailedEvent())
}
})
}
答案 0 :(得分:1)
您的标题不正确。
您可以删除标题Content-type: application/x-www-form-urlencoded
或者您应该更改为Content-type: multipart/form-data