【问题标题】:invalid query parameters: invalid %-encoding无效的查询参数:无效的 %-encoding
【发布时间】:2019-07-19 05:47:19
【问题描述】:

我尝试通过 Retrofit / Multipart 上传图片(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?>

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

查尔斯回应:

【问题讨论】:

    标签: android kotlin retrofit2 multipartform-data okhttp3


    【解决方案1】:

    您的标题不正确。

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

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

    【讨论】:

    • 我的上帝...我尝试了各种内容类型,但没有任何效果。现在我完全删除了 Content-type 并且它可以工作。你救了我的一天!谢谢
    猜你喜欢
    • 1970-01-01
    • 2019-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多