gpt4 book ai didi

ios - Error Domain=NSURLErrorDomain Code=-1005 "网络连接丢失

转载 作者:行者123 更新时间:2023-11-28 15:59:38 24 4
gpt4 key购买 nike

我在使用 SwiftyBeaver 记录器时遇到此错误,它试图通过以下代码将数据发送到云端:

func sendToServerAsync(str: String?, complete: (ok: Bool, status: Int) -> ()) {

if let payload = str, let queue = self.queue {

// create operation queue which uses current serial queue of destination
let operationQueue = NSOperationQueue()
operationQueue.underlyingQueue = queue

let session = NSURLSession(configuration:
NSURLSessionConfiguration.defaultSessionConfiguration(),
delegate: nil, delegateQueue: operationQueue)

// assemble request
let request = NSMutableURLRequest(URL: serverURL)
request.HTTPMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

// basic auth header
let credentials = "\(appID):\(appSecret)".dataUsingEncoding(NSUTF8StringEncoding)!
let base64Credentials = credentials.base64EncodedStringWithOptions([])
request.setValue("Basic \(base64Credentials)", forHTTPHeaderField: "Authorization")

// POST parameters
let params = ["payload": payload]
do {
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: [])
} catch let error as NSError {
toNSLog("Error! Could not create JSON for server payload. \(error)")
}
//toNSLog("sending params: \(params)")
//toNSLog("\n\nbefore sendToServer on thread '\(threadName())'")

sendingInProgress = true
// send request async to server on destination queue
let task = session.dataTaskWithRequest(request) {
_, response, error in
var ok = false
var status = 0
//toNSLog("callback of sendToServer on thread '\(self.threadName())'")

if let error = error {
// an error did occur
self.toNSLog("Error! Could not send entries to server. \(error)")
} else {
if let response = response as? NSHTTPURLResponse {
status = response.statusCode
if status == 200 {
// all went well, entries were uploaded to server
ok = true
} else {
// status code was not 200
var msg = "Error! Sending entries to server failed "
msg += "with status code \(status)"
self.toNSLog(msg)
}
}
}
return complete(ok: ok, status: status)
}
task.resume()
}
}

奇怪的是它对前两个或三个日志条目有效,然后由于上述错误而停止。我尝试重置模拟器上的内容和设置并重新启动我的模拟器(如 Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." 中所建议),但这只是暂时修复它——在前 2-3 个日志条目之后,它再次开始失败。

昨晚我尝试与 SwiftBeaver 的创建者一起调试了几个小时,但我们无法让它工作。似乎没有多少人看到这个问题。

我尝试移除我的 Wifi 连接并重新连接,但这也不起作用。

任何关于这方面的指导将不胜感激。

仅供引用,我使用的是 Swift 2 和 XCode 7.3。

最佳答案

这可能是由于 HTTP keep-alive 支持在 iOS 模拟器中存在严重错误。见:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

有关更多详细信息,但简短的回答是在您进行模拟器测试时在您使用的服务器上禁用保持事件状态,或者更好的是,添加一些逻辑,以便在发现特定错误时立即重试请求。

关于ios - Error Domain=NSURLErrorDomain Code=-1005 "网络连接丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41274326/

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