xcode Alamofire 请求错误:NSURLErrorDomain 代码 -1005 网络连接丢失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33816116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alamofire Request error: NSURLErrorDomain Code -1005 The network connection was lost
提问by Cody Winton
I'm working on transferring my project from AFNetworking to Alamofire. Really like the project. However, I'm receiving this error when attempting to make a GET request.
我正在将我的项目从 AFNetworking 转移到 Alamofire。真的很喜欢这个项目。但是,我在尝试发出 GET 请求时收到此错误。
Here's some example code:
下面是一些示例代码:
class func listCloudCredntials(onlyNew onlyNew: Bool = true, includePending: Bool = true) -> Request {
let parameters: [String: AnyObject] = includePending ? ["include_pending": "true"] : [:]
let urlString = "https://myapp-staging.herokuapp.com/api/1/credntials"
let token = SSKeychain.storedToken()
let headers: [String: String] = ["Authorization": "Bearer \(token)"]
return Alamofire.request(.GET, urlString, parameters: parameters, encoding: .JSON, headers: headers)
}
Then, in my VC:
然后,在我的 VC 中:
listCloudCredntials().validate().responseJSON() {
(response: Response<AnyObject, NSError>) in
switch response.result {
case .Success(let result):
printCR("success: \(result)")
case .Failure(let error):
printCR("error: \(error)")
}
}
Here's the error I'm running into:
这是我遇到的错误:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x14e9c77f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://myapp-staging.herokuapp.com/api/1/credntials, NSErrorFailingURLKey=https://myapp-staging.herokuapp.com/api/1/credntials, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}
错误域=NSURLErrorDomain 代码=-1005“网络连接丢失。” UserInfo={NSUnderlyingError=0x14e9c77f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey= https://myapp-staging.herokuapp.com/api 1/credntials, NSErrorFailingURLKey= https://myapp-staging.herokuapp.com/api/1/credntials, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=网络连接丢失。}
I've tried running on the iOS Simulator with 1OS 8.4 and iOS 9.1, as well as my iPhone 6S running iOS 9.1.
我试过在装有 1OS 8.4 和 iOS 9.1 的 iOS 模拟器上运行,以及在运行 iOS 9.1 的 iPhone 6S 上运行。
What am I doing wrong?
我究竟做错了什么?
--------EDIT--------
--------编辑--------
To clarify, this function works just fine with AFNetworking.
澄清一下,这个函数与 AFNetworking 一起工作得很好。
Here's the debugprint(request) result (it's a GET request):
这是 debugprint(request) 结果(这是一个 GET 请求):
$ curl -i \
-H "Authorization: Bearer some-JWT-Security-Token" \
-H "Content-Type: application/json" \
-H "Accept-Language: en-US;q=1.0" \
-H "Accept-Encoding: gzip;q=1.0,compress;q=0.5" \
-H "User-Agent: Company/com.Company.AppName (1081; OS Version 9.2 (Build 13C75))" \
-d "{\"include_pending\":\"true\"}" \
"https://appname-staging.herokuapp.com/api/1/credntials"
I have to change curl -i to curl -X GET in order for the curl to return successfully.
我必须将 curl -i 更改为 curl -X GET 才能使 curl 成功返回。
Here is another call that I'm required to make in the app that works with no issues.
这是我需要在没有问题的应用程序中进行的另一个调用。
curl -i \
-X POST \
-H "Content-Type: application/json" \
-H "Accept-Language: en-US;q=1.0" \
-H "Accept-Encoding: gzip;q=1.0,compress;q=0.5" \
-H "User-Agent: Company/com.Company.AppName (1081; OS Version 9.2 (Build 13C75))" \
-d "{\"token\":\"UserSessionTokenString\"}" \
"https://appname-staging.herokuapp.com/api/1/authenticate/user"
Could it be something with the GET vs. POST?
这可能是 GET 与 POST 的关系吗?
回答by Cody Winton
Resolved! I was using JSON encoding for a GET request. Here's the answer: Alamofire Request error only on GET requests
解决!我对 GET 请求使用 JSON 编码。答案如下:Alamofire 请求错误仅在 GET 请求中出现
回答by Horst
If you retain the request in your vc or other object, this error would be gone.
如果您将请求保留在 vc 或其他对象中,则此错误将消失。