将数据发布到Google文本到语音时,HTTP状态400接收到无效的JSON负载

时间:2018-06-22 03:39:28

标签: java google-cloud-platform google-text-to-speech

这是我的代码,使用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字符串,并且它是有效的。

2 个答案:

答案 0 :(得分:0)

我相信这里的问题实际上与您发布的JSON有关。避免用反斜杠"引起引号符号\的转义。那应该解决您的问题。简而言之,您在此处发布的JSON应该看起来像这样:

  

{“ input”:{“ text”:“ Android是由以下公司开发的移动操作系统   Google,基于Linux内核,主要设计用于   触摸屏移动设备,例如智能手机和平板电脑。“},”语音“:   {“ languageCode”:“ en-gb”,“ name”:“ en-GB-Standard-A”,“ ssmlGender”:   “ FEMALE”},“ audioConfig”:{“ audioEncoding”:“ MP3”}}

答案 1 :(得分:0)

检查HTTP标头

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

内容类型:application / json