我有一个api,使用时我会从请求中获取一个数组。现在,我想将该数组传递给另一个API请求,但效果不佳。下面是我的代码
func insertENCR(value: NSArray){
let url = URL(string: String.offers)
let request = NSMutableURLRequest(url: url!)
request.httpMethod = "POST"
let postParameters = ["action":"encrMSISDNS", "msisdns":value, "username":username!, "os":getAppVersion()]
print("postEncr \(postParameters)")
if let jsonParameters = try? JSONSerialization.data(withJSONObject: postParameters, options: .prettyPrinted){
let theJSONText = String(data: jsonParameters,encoding: String.Encoding.utf8)
let requestBody: Dictionary<String, Any> = [
"requestBody":encryptAsyncRequest(requestBody: theJSONText!.description)
]
if let postData = (try? JSONSerialization.data(withJSONObject: requestBody, options: JSONSerialization.WritingOptions.prettyPrinted)){
request.httpBody = postData
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
//passing the response
do{
//converting the response to NSDictionary
let myJSON = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = myJSON{
var responseBody: String?
responseBody = parseJSON["responseBody"] as! String?
// print("responseBody:: \(responseBody ?? "")")
if let resBody = responseBody{
print("response raw msisdn:: \(decryptedResponseBody)")
}
}
}catch {
print(error.localizedDescription)
}
}
task.resume()
}
}
}
请求有效负载中的msisdn键需要根据api结构的数组。
请仔细阅读错误
异构集合文字只能推断为'[String:Any]';如果有意,则添加显式类型注释
它甚至包含解决方案:添加显式类型注释
let postParameters : [String:Any] = ["action":"encrMSISDNS", "msisdns":value, "username":username!, "os":getAppVersion()]
将数据发送到服务器时,建议不要使用NS...
收集类型,请不要prettyPrint。服务器不在乎。