无效的查询参数:无效的%编码

时间:2019-02-25 18:39:34

标签: android kotlin retrofit2 multipartform-data okhttp3

我尝试通过翻新/分段上传图片(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())
        }
    })
}

查尔斯回应: Charles response

1 个答案:

答案 0 :(得分:1)

您的标题不正确。

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

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