gpt4 book ai didi

ios - 无法在 swift 3 中发布请求参数?

转载 作者:行者123 更新时间:2023-11-30 12:03:10 26 4
gpt4 key购买 nike

这里我使用这个 Json 函数从 api 获取数据,但是在 let task = URLSession.shared.dataTask(with: request) { data, response, error in 这行之后,它移到了最后大括号并且不执行中间的剩余行,输出将是带有字典的数组,任何人都可以帮助我如何获得对此的响应?

func shippingmethodURL(shippingMethodAPI:String) {
let url = NSURL(string: shippingMethodAPI)
var request = URLRequest(url: url! as URL)
request.httpMethod = "POST"
print(shippingMethodAPI)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let addtoCartVC = storyboard.instantiateViewController(withIdentifier: "checkout") as! CheckoutViewController
let parameters : [String: Any] = ["address":
[ "region": "California",
"region_code": "CA",
"region_id": "12",
"country_id": "US",
"company": "Test",
"telephone": "9492162752",
"postcode": "43",
"city": "Chennai",
"firstname": "gdfgdgdfg",
"lastname": "dgdfgdfgg",
"email": "sfdsfsdf@gmail.com",
"prefix": "",
"sameAsBilling": 1,
"street": ["Dsfdsfsd dfdsfdsf dsfsfdsfsf sdfsfdsfsdfC"]]]
print(parameters)
do {
request.httpBody = try JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted)

} catch let error {
print(error.localizedDescription)
}
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
print(request)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(String(describing: error))")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString!)")
let status = (response as! HTTPURLResponse).statusCode
self.keyStatusCode = status
print(status)
}
}

最佳答案

当您使用 dataTask 方法时,您将返回一个任务对象。所以你需要对其调用resume()

创建任务后,必须通过调用其 resume() 方法来启动它。该任务调用 session 委托(delegate)上的方法来为您提供响应元数据、响应数据等。

Learn about :

关于ios - 无法在 swift 3 中发布请求参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46974540/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com