4

我正在尝试使用 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));

谢谢你的帮助

4

1 回答 1

7

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

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

还应该注意的是,谷歌建议将您的图像大小调整为 1024 x 768 - 您可以使用以下方法调整图像大小:

img = img.imageAsResized(1024,768);

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

于 2017-05-16T03:27:05.453 回答