首页 > 解决方案 > 将数据发布到谷歌文本到语音时收到的 Http 状态 400 接收到无效的 JSON 有效负载

问题描述

这是我使用 jsoup 发布 json 数据的代码

  Response resp = Jsoup.connect("https://texttospeech.googleapis.com/v1beta1/text:synthesize")
  .header("Authorization", " Bearer "+credential.getAccessToken())
  .header("Content-Type", "application/json; charset=UTF-8")

  .ignoreContentType(true)
  .method(Method.POST)
  .ignoreHttpErrors(true)
  .requestBody("{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.\" }, \"voice\":{ \"languageCode\":\"en-gb\", \"name\":\"en-GB-Standard-A\", \"ssmlGender\":\"FEMALE\" }, \"audioConfig\":{ \"audioEncoding\":\"MP3\" } }")
  .execute();

这是错误消息:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.\" }, \"voice\":{ \"languageCode\":\"en-gb\", \"name\":\"en-GB-Standard-A\", \"ssmlGender\":\"FEMALE\" }, \"audioConfig\":{ \"audioEncoding\":\"MP3\" } }\": Cannot bind query parameter. Field '{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets' could not be found in request message.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.\" }, \"voice\":{ \"languageCode\":\"en-gb\", \"name\":\"en-GB-Standard-A\", \"ssmlGender\":\"FEMALE\" }, \"audioConfig\":{ \"audioEncoding\":\"MP3\" } }\": Cannot bind query parameter. Field '{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets' could not be found in request message."
          }
        ]
      }
    ]
  }
}

我认为 json 字符串无效,但不是,我验证 json 字符串,并且它有效。

标签: javagoogle-cloud-platformgoogle-text-to-speech

解决方案


检查 HTTP 标头

确保您通过以下方式发送请求:

内容类型:应用程序/json


推荐阅读