【问题标题】:Why do I get invalid JSON payload when calling google cloud vision API from appcelerator?为什么从 appcelerator 调用谷歌云视觉 API 时得到无效的 JSON 有效负载?
【发布时间】:2016-03-09 07:47:27
【问题描述】:

我正在尝试使用 Alloy Appcelerator 的 Google vision API v1

我创建一个请求 HTTPClient 并调用 API https://vision.googleapis.com/v1/images:annotate?key=MY_APP_KEY

但我从谷歌得到了回复文本:

  {
 error = {
     code = 400;
     details = (
                  {
                     "@type" = "type.googleapis.com/google.rpc.BadRequest";
                      fieldViolations = ({
                                        description = "Invalid JSON payload received. Unknown name \"request\": Cannot bind query parameter. Field 'request' could not be found in request message.";
                                        });
                  }
                );
     message = "Invalid JSON payload received. Unknown name \"request\": Cannot bind query parameter. Field 'request' could not be found in request message.";
     status = "INVALID_ARGUMENT";
  };

}

还有我的代码使用 Alloy 的 HTTP 请求

var requests =  
{
  "requests":[
    {
      "image":{
        "content": "image_have_encodebase64",
      },
      "features":[
        {
          "type":"TEXT_DETECTION",
          "maxResults":1
        }
      ]
    }
  ]
};
var xhr = Titanium.Network.createHTTPClient();
xhr.open("POST", 'https://vision.googleapis.com/v1/images:annotate?key=MY_APP_KEY');
xhr.send(JSON.stringify(requests));

感谢您的帮助

【问题讨论】:

  • 查看API,我注意到数组名称应该是requests(复数)而不是request。让我知道它是否有帮助
  • 感谢您的帮助,但如果我将请求更改为请求,则会出现同样的错误
  • 你试过 JSON.stringify() 了吗?喜欢:xhr.send(JSON.stringify(requests));
  • 嗨 Carlos Henrique Lustosa,我尝试这种方式但没有成功
  • 亲爱的,我已经修好了。因为我encode64的字符串长度太长了,所以发不出去。我必须为标题设置内容长度。感谢@Fokke-Appcelerator - Appcelerator,Carlos Henrique Lustosa 的支持

标签: google-api appcelerator appcelerator-titanium google-vision


【解决方案1】:

通过设置 Content-LengthContent-Type 标头,它应该可以工作:

xhr.setRequestHeader("Content-Length", size);
xhr.setRequestHeader("Content-Type", "application/json");

还应注意,Google 建议将您的图片大小调整为 1024 x 768 - 您可以使用以下方式调整图片大小:

img = img.imageAsResized(1024,768);

对我的代码进行这些更改后,我一切正常。

【讨论】:

    猜你喜欢
    • 2017-02-26
    • 2018-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多