查询参数无效:%-encoding无效

问题描述 投票:0回答:1

我尝试通过Retrofit / Multipart上传图片(jpg)。 Api总是返回代码400,其中包含“无效的查询参数:无效的%-encoding”我不知道什么是错误的。

我的界面:

@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?>

我的webservice上传照片方法:

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())
        }
    })
}

查尔斯的回应:Charles response

android kotlin retrofit2 multipartform-data okhttp3
1个回答
1
投票

你的标题不正确。

您可以删除标题Content-type: application/x-www-form-urlencoded

或者你应该改为Content-type: multipart/form-data

© www.soinside.com 2019 - 2024. All rights reserved.