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

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

问题描述:

我正在将我的项目从AFNetworking转移到Alamofire。真的很喜欢这个项目。但是,尝试发出GET请求时收到此错误。

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.

下面是一些示例代码:

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)
}

然后,在我的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)")
    }
}

这是我遇到的错误:


错误域= 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 =网络连接丢失。}

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.}

我尝试在iOS模拟器上运行1OS 8.4和iOS 9.1,以及运行iOS 9.1的iPhone 6S。

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.

我在做什么错了?

-------- 编辑 --------

--------EDIT--------

此功能需要澄清

这是debugprint(request)结果(它是一个GET请求):

Here's the debugprint(request) result (it's a GET request):

$ 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"

我必须将curl -i更改为curl- X GET,以便成功恢复卷发。

I have to change curl -i to curl -X GET in order for the curl to return successfully.

这里是我需要在没有任何问题的应用中进行的另一个调用。

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"

是GET与POST的结合吗?

Could it be something with the GET vs. POST?

已解决!对GET请求使用JSON编码。答案是:Alamofire仅在GET请求上出现请求错误

Resolved! I was using JSON encoding for a GET request. Here's the answer: Alamofire Request error only on GET requests