简体   繁体   English

Alamofire请求错误:NSURLErrorDomain代码-1005网络连接丢失

[英]Alamofire Request error: NSURLErrorDomain Code -1005 The network connection was lost

I'm working on transferring my project from AFNetworking to Alamofire. 我正在努力将我的项目从AFNetworking转移到Alamofire。 Really like the project. 真的很喜欢这个项目。 However, I'm receiving this error when attempting to make a GET request. 但是,我在尝试发出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." 错误域= 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=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 =网络连接丢失。}

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. 我尝试在iOS模拟器上运行1OS 8.4和iOS 9.1,以及运行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(请求)结果(它是一个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的关系吗?

Resolved! 解决! I was using JSON encoding for a GET request. 我正在使用JSON编码进行GET请求。 Here's the answer: Alamofire Request error only on GET requests 答案如下: Alamofire仅在GET请求时请求错误

如果您在vc或其他对象中保留请求,则此错误将消失。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Error Domain = NSURLErrorDomain代码= -1005“网络连接丢失 - Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost NSURLErrorDomain代码= -1005“网络连接丢失 - NSURLErrorDomain Code=-1005 "The network connection was lost 网络连接丢失。NSURLErrorDomain代码= -1005 - The network connection was lost.NSURLErrorDomain Code=-1005 名为“ NSURLErrorDomain代码= -1005”的网络连接丢失。 - Network error named “NSURLErrorDomain Code=-1005 ”The network connection was lost." comes too frequently 在移动网络上解析iOS SDK错误域= NSURLErrorDomain代码= -1005“网络连接丢失。” - Parse iOS SDK Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” on mobile network NSURLErrorDomain代码= -1005“网络连接丢失。”,_ kCFStreamErrorCodeKey = 54 - NSURLErrorDomain Code=-1005 “The network connection was lost.”, _kCFStreamErrorCodeKey=54 无法确定当前的国家/地区代码:错误域= NSURLErrorDomain代码= -1005“网络连接丢失。” - Could not determine current country code: Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” 为什么NSURLConnection失败,错误域= NSURLErrorDomain Code = -1005“网络连接丢失。”在Swift iOS8中? - Why NSURLConnection failed with Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” in Swift iOS8? Error Domain = NSURLErrorDomain代码= -1005“网络连接丢失。”-在iOS 9中使用https - Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” - Occurs in iOS 9 with https 随机获取Error Domain = NSURLErrorDomain代码= -1005“网络连接丢失。” - Randomly getting Error Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM