kotlinx.serialization.SerializationException:找不到类“MultiPartFormDataContent”的序列化程序

时间:2021-01-21 11:01:23

标签: android ktor kotlinx.serialization ktor-client kotlin-multiplatform-mobile

我正在尝试上传多个文件。

val ktorVersion = "1.5.0"
val serializationVersion = "1.0.1"

我就是这样做的:

  override suspend fun uploadFiles(
    binaryFiles: Map<String,ByteArray>
  ): BaseResponse<List<String>> {
    return client.submitForm {

      url(fileUploadUrl)
      method = HttpMethod.Post
      body = MultiPartFormDataContent(
        formData {
          headers{
            append("Content-Type", "application/json")
            append("Authorization", "Bearer $token}")
          }
          binaryFiles.entries.forEach {
            append(
              key = "files",
              value = it.value,
              headers = Headers.build {
                append(HttpHeaders.ContentDisposition, "filename=${it.key}")
              }
            )
          }
        }
      )
    }
  }

但它抛出异常

kotlinx.serialization.SerializationException: Serializer for class 'MultiPartFormDataContent' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
    at kotlinx.serialization.internal.Platform_commonKt.serializerNotRegistered(Platform.common.kt:91)
    at kotlinx.serialization.SerializersKt__SerializersKt.serializer(Serializers.kt:130)
    at kotlinx.serialization.SerializersKt.serializer(Unknown Source)
    at io.ktor.client.features.json.serializer.KotlinxSerializerKt.buildSerializer(KotlinxSerializer.kt:82)
    at io.ktor.client.features.json.serializer.KotlinxSerializerKt.access$buildSerializer(KotlinxSerializer.kt:1)
    at io.ktor.client.features.json.serializer.KotlinxSerializer.writeContent$ktor_client_serialization(KotlinxSe
    at io.ktor.client.features.json.serializer.KotlinxSerializer.write(KotlinxSerializer.kt:26)
    at io.ktor.client.features.json.JsonFeature$Feature$install$1.invokeSuspend(JsonFeature.kt:150)
    at io.ktor.client.features.json.JsonFeature$Feature$install$1.invoke(JsonFeature.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceedWith(SuspendFunctionGun.kt:123)
    at io.ktor.client.features.HttpCallValidator$Companion$install$1.invokeSuspend(HttpCallValidator.kt:106)
    at io.ktor.client.features.HttpCallValidator$Companion$install$1.invoke(HttpCallValidator.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
    at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invokeSuspend(HttpRequestLifecycle.kt:37)
    at io.ktor.client.features.HttpRequestLifecycle$Feature$install$1.invoke(HttpRequestLifecycle.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:243)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:113)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:133)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:79)
    at io.ktor.client.HttpClient.execute(HttpClient.kt:187)
    at io.ktor.client.statement.HttpStatement.executeUnsafe(HttpStatement.kt:104)
    at com.example.package.core.data.network.services.upload.FileUploadApiImpl.uploadFiles(FileUploadApi.kt:99)

我也试过这种方法,但遇到了同样的问题:

  suspend fun uploadFilesTest(
    binaryFiles: Map<String,ByteArray>
  ): BaseResponse<List<String>> {
    return client.post(fileUploadUrl) {
      headers {
        append("Content-Type", ContentType.Application.Json)
        append("Authorization", "Bearer $token")
      }
      body = MultiPartFormDataContent(
        formData {
          binaryFiles.entries.forEach {
            this.appendInput(
              key = "files",
              size = it.value.size.toLong(),
              headers = Headers.build {
                append(HttpHeaders.ContentDisposition, "filename=${it.key}")
              },
            ){ buildPacket { writeFully(it.value) }}
          }
        }
      )
    }
  }

1 个答案:

答案 0 :(得分:1)

看看你说你有同样问题的最后一个片段,你似乎在这样的标题中指定内容类型

append("Content-Type", ContentType.Application.Json)

但您还希望通过将 body 设置为 MultiPartFormDataContent 使其成为多部分,这也需要定义为标题 content-type:multipart/form-data; ... 因此可能是此冲突导致了问题。

我实际上只是在我的代码中自己尝试过,这似乎是我的问题。