gpt4 book ai didi

ios - NSURL 请求不起作用

转载 作者:行者123 更新时间:2023-11-30 13:25:00 27 4
gpt4 key购买 nike

我有一个请求,之后我需要一本完整的词典。但是当我用断点检查我的请求时,我发现它跳过了我的请求......这有什么问题?

  func downloadPackages() {
let url = "\(URL_BASE)package/list?id=666"
let dataURL = NSURL(string: url)
let dataRequest = NSURLRequest(URL: dataURL!)
let dataSession = NSURLSession.sharedSession()
print("QQ")
let dataTask = dataSession.dataTaskWithRequest(dataRequest) { (data, response, error) -> Void in
print("QQ2")

if error != nil {
print(error.debugDescription)
} else {
do {
let packageDict = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? Dictionary<String, AnyObject>
if let results = packageDict!["data"] as? [Dictionary<String, AnyObject>] {
for obj in results {
let package = Package(packageDict: obj)
print(self.monsters.count)
self.monsters.append(package)

}
}
//Main UI thread
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
catch {
}
}
}
dataTask.resume()
}

它不打印(“QQ2”),但打印(“QQ”)。

最佳答案

我认为您没有添加 HTTP 请求方法和值。所以尝试这样:-

func downloadPackages() {
let url = "\(URL_BASE)package/list?id=666"
let dataURL = NSURL(string: url)
let dataRequest: NSMutableURLRequest = NSMutableURLRequest(URL: dataURL!)
dataRequest.HTTPMethod = "POST"
dataRequest.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
dataRequest.setValue("*/*", forHTTPHeaderField: "Accept")
let dataSession = NSURLSession.sharedSession()
print("QQ")
let dataTask = dataSession.dataTaskWithRequest(dataRequest) { (data, response, error) -> Void in
print("QQ2")

if error != nil {
print(error.debugDescription)
} else {
do {
let packageDict = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) as? Dictionary<String, AnyObject>
if let results = packageDict!["data"] as? [Dictionary<String, AnyObject>] {
for obj in results {
let package = Package(packageDict: obj)
print(self.monsters.count)
self.monsters.append(package)

}
}
//Main UI thread
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
catch {
}
}
}
dataTask.resume()
}

关于ios - NSURL 请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338491/

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