gpt4 book ai didi

ios - 如果服务器没有应答则超时

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

我的 VC 中有这样的代码:

    if Reachability.isConnectedToNetwork() == true {
print("Internet connection OK")

JSONParseFunc()

} else {
print("Internet connection FAILED")
let alert = UIAlertView(title: "you're not conneted to internet ", message: "make sure you're connected to server ", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}

其中我检查互联网的可达性,但我想检查服务器是否正在发送数据,我需要为此目的设置超时,如何在这段代码中实现超时,这是我的JSON函数:

func JSONParseFunc(){



let requestURL: NSURL = NSURL(string: "XXXXXXX")!
let urlRequest: NSMutableURLRequest = NSMutableURLRequest(url: requestURL as URL)
let session = URLSession.shared
let task = session.dataTask(with: urlRequest as URLRequest) {
(data, response, error) -> Void in


let readableJSON = JSON(data: data! , options: JSONSerialization.ReadingOptions.mutableContainers, error: nil)

self.nrows = readableJSON.count

for i in 0...self.nrows{

if let Name = readableJSON[i]["Name"].string as String! {
var ID = readableJSON[i]["Id"].intValue
var iDString = String (ID)

var desc = readableJSON[i]["Description"].string as String!


self.iDArray.append(iDString)

self.description1.append(desc!)
self.namesArray.append(Name)


}




}

self.tableView.reloadData()
}
task.resume()

self.tableView.reloadData()


}

最佳答案

尝试这样设置

 var session = URLSession()
override init() {
let configuration = URLSessionConfiguration.default
configuration.timeoutIntervalForRequest = 30.0
configuration.timeoutIntervalForResource = 60.0
session = URLSession(configuration: configuration)

}

关于ios - 如果服务器没有应答则超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43756363/

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