gpt4 book ai didi

ios - 快速发出 GET 请求时无法到达端点

转载 作者:行者123 更新时间:2023-11-29 00:36:38 24 4
gpt4 key购买 nike

你好,我正在尝试从我的服务器发出一个获取请求,但是我没有到达端点,因为我检查了我的日志,我什至没有到达最终端点。端点需要已添加到请求字段的授权。我正在尝试获取 json 值并将它们显示为表格单元格。但是当我尝试检索值时它返回零。它从不尝试到达终点。我尝试放置打印语句 print("Session)" 来检查它是否调用 dataWithRequest 但是否失败。请问我在这里做错了什么吗?

 func getLocalData() -> [[String:AnyObject]] {
var json:[[String:AnyObject]]?
let endPoint = "http://******************"

let url:NSURL? = NSURL(string: endPoint)
let session = NSURLSession.sharedSession()

let request:NSMutableURLRequest = NSMutableURLRequest(URL: url!)

request.HTTPMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(token!, forHTTPHeaderField: "Authorization-Header")


let task = session.dataTaskWithRequest(request) {(data, response, error) -> Void in
print("Session") //not able to get in here
guard let data = data where error == nil else {
//network error
print("Error with network: \(error?.localizedDescription)")
return
}

let httpResponse = response as! NSHTTPURLResponse//HTTPURLResponse
let statusCode = httpResponse.statusCode

if statusCode == 200 {
do {
print("Here buddy")
json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [[String: AnyObject]]
// return json
}

catch {
print("Error in json")
}
}
}

task.resume()

print("JSON: \(json)") //prints nil as it never attempts to make the request to the server

return json
}

在我的 TableListController 中,我尝试调用此函数,它提示 fatal error: unexpectedly found nil while unwrapping an Optional value

class DealListController: UITableViewController {
let myDeals = DealList()
var dealsArray = []

override func awakeFromNib() {
super.awakeFromNib()
dealsArray = myDeals.getLocalData()! //complains about error here
}
}

最佳答案

让我们将这些东西添加到您的 Info.plist 中 enter image description here

更新:让我们使用完成处理程序而不是返回值

func getLocalData(completion: (json: [[String:AnyObject]]?) -> Void) {
let endPoint = "http://******************"

let url:NSURL? = NSURL(string: endPoint)
let session = NSURLSession.sharedSession()

let request:NSMutableURLRequest = NSMutableURLRequest(URL: url!)

request.HTTPMethod = "GET"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("", forHTTPHeaderField: "Authorization-Header")


let task = session.dataTaskWithRequest(request) {(data, response, error) -> Void in
print("Session") //not able to get in here
guard let data = data where error == nil else {
//network error
print("Error with network: \(error?.localizedDescription)")
completion(json: nil)
return
}

let httpResponse = response as! NSHTTPURLResponse//HTTPURLResponse
let statusCode = httpResponse.statusCode

if statusCode == 200 {
do {
print("Here buddy")
let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as? [[String: AnyObject]]
// return json
completion(json: json)
}

catch {
print("Error in json")
completion(json: nil)
}
}
}

task.resume()
}

和:

myDeals.getLocalData { json in
print("json: \(json)")
dealsArray = json!
}

关于ios - 快速发出 GET 请求时无法到达端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40445401/

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