Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 带有Google Vision API的请求-接收到无效的JSON负载和无效的\u参数_Ios_Json_Google Api_Http Post_Google Vision - Fatal编程技术网

Ios 带有Google Vision API的请求-接收到无效的JSON负载和无效的\u参数

Ios 带有Google Vision API的请求-接收到无效的JSON负载和无效的\u参数,ios,json,google-api,http-post,google-vision,Ios,Json,Google Api,Http Post,Google Vision,尝试按照中的说明从iOS应用程序创建请求,但收到无效的JSON有效负载和无效的\u参数错误消息。有什么建议吗 下面是我尝试使用的示例 { "requests":[ { "image":{ "source":{ "imageUri": "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"

尝试按照中的说明从iOS应用程序创建请求,但收到无效的JSON有效负载和无效的\u参数错误消息。有什么建议吗

下面是我尝试使用的示例

{
  "requests":[
    {
      "image":{
        "source":{
          "imageUri":
            "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"
        }
      },
      "features":[
        {
          "type":"LOGO_DETECTION",
          "maxResults":1
        }
      ]
    }
  ]
}
在我的iOS应用程序中,我创建了

let feature1 = ["type": "LOGO_DETECTION", "maxResults": 1] as [String : Any]
let features = [feature1]
let source = ["imageUri": "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png"]
let image = ["source": source]
let request = ["image": image, "features": features] as [String : Any]
let requests = ["requests": [request]]

if let data = try? JSONSerialization.data(withJSONObject: requests, options: .prettyPrinted),
    let str = String(data: data, encoding: .utf8) {
    print(str)
}

let privateKey = "my............key"

let requestString = "https://vision.googleapis.com/v1/images:annotate?key=\(privateKey)"
Alamofire.request(requestString, method: .post, parameters: requests).responseString { (response) in
    switch response.result {
    case .success(let data):
        print("data", data)
        print(response.result)
    case .failure(let error):
        print(error)
    }
}
我可以看到我的打印日志中有同样的东西

{
  "requests" : [
    {
      "features" : [
        {
          "type" : "LOGO_DETECTION",
          "maxResults" : 1
        }
      ],
      "image" : {
        "source" : {
          "imageUri" : "https:\/\/www.google.com\/images\/branding\/googlelogo\/2x\/googlelogo_color_272x92dp.png"
        }
      }
    }
  ]
}
但我收到下面的错误消息

[BoringSSL] Function boringssl_context_get_peer_sct_list: line 1754 received sct extension length is less than sct data length
data {
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"requests[][features][][maxResults]\": Cannot bind query parameter. Field 'requests[][features][][maxResults]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"requests[][image][source][imageUri]\": Cannot bind query parameter. Field 'requests[][image][source][imageUri]' could not be found in request message.\nInvalid JSON payload received. Unknown name \"requests[][features][][type]\": Cannot bind query parameter. Field 'requests[][features][][type]' 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 \"requests[][features][][maxResults]\": Cannot bind query parameter. Field 'requests[][features][][maxResults]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"requests[][image][source][imageUri]\": Cannot bind query parameter. Field 'requests[][image][source][imageUri]' could not be found in request message."
          },
          {
            "description": "Invalid JSON payload received. Unknown name \"requests[][features][][type]\": Cannot bind query parameter. Field 'requests[][features][][type]' could not be found in request message."
          }
        ]
      }
    ]
  }
}
正确答案

1.移除

if let data = try? JSONSerialization.data(withJSONObject: requests, options: .prettyPrinted),
    let str = String(data: data, encoding: .utf8) {
    print(str)
}
2.改变

Alamofire.request(requestString, method: .post, parameters: requests).responseString { (response) in


乍一看,错误非常明显:

未知名称请求[][features][[maxResults]:无法绑定查询参数。在请求消息中找不到字段。 未知名称请求[][image][source][imageUri]:无法绑定查询参数。在请求消息中找不到字段。 未知名称请求[][features][[type]:无法绑定查询参数。在请求消息中找不到字段。 但将您的控制台输出与文档中的示例进行比较,结果是一致的。我猜问题是两种可能性之一:

请求中完全缺少有效负载,或者 您没有正确编码有效负载。 为了在下次发生这种情况时帮助您,我建议您通过构建一个工作负载并通过Curl或Postman发送它来进行调试,以消除您的应用程序是罪魁祸首

一旦您拥有有效的有效负载,请将您的Swift应用程序指向并将您的请求发送到该应用程序。这将允许您检查发送的内容,并将其与工作示例进行比较

现在来解决它:

原来问题是2-您没有正确编码它。或者更确切地说,您是,但您没有发送编码字符串

请看这一行:

let requests = ["requests": [request]]

if let data = try? JSONSerialization.data(withJSONObject: requests, options: .prettyPrinted),
    let str = String(data: data, encoding: .utf8) {
    print(str)
}
然后你会:

Alamofire.request(requestString, method: .post, parameters: requests).responseString { (response) in
注意您是如何在有效负载中使用请求的,而不是编码的str?您已将编码字符串隐藏在if语句中,使其无法使用。我在这里使用的是guard,它的代码更少,而且不会隐藏编码字符串,而且更安全:

let requests = ["requests": [request]]

guard let data = try? JSONSerialization.data(withJSONObject: requests, options: .prettyPrinted),
      let encodedStr = String(data: data, encoding: .utf8) else { return }

Alamofire.request(requestString, method: .post, parameters: encodedStr).responseString { (response) in
                                                            // ^ use the right var here!

哦你是对的。修复了调用中额外参数“method”的错误,如果我将encodedStr放入参数soh god中。我们不需要自己对请求进行编码。请参阅我的编辑。感谢您帮助调试我的代码。我会发布答案:是的,你也可以这样做;
let requests = ["requests": [request]]

guard let data = try? JSONSerialization.data(withJSONObject: requests, options: .prettyPrinted),
      let encodedStr = String(data: data, encoding: .utf8) else { return }

Alamofire.request(requestString, method: .post, parameters: encodedStr).responseString { (response) in
                                                            // ^ use the right var here!