簡體   English   中英

Alamofire請求錯誤:NSURLErrorDomain代碼-1005網絡連接丟失

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

我正在努力將我的項目從AFNetworking轉移到Alamofire。 真的很喜歡這個項目。 但是,我在嘗試發出GET請求時收到此錯誤。

這是一些示例代碼:

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 =網絡連接丟失。}

我嘗試在iOS模擬器上運行1OS 8.4和iOS 9.1,以及運行iOS 9.1的iPhone 6S。

我究竟做錯了什么?

-------- 編輯 --------

為了澄清,這個功能與AFNetworking一起工作得很好。

這是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"

我必須將curl -i更改為curl -X GET才能使curl成功返回。

這是我需要在應用程序中進行的另一個調用,無任何問題。

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的關系嗎?

解決! 我正在使用JSON編碼進行GET請求。 答案如下: Alamofire僅在GET請求時請求錯誤

如果您在vc或其他對象中保留請求,則此錯誤將消失。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM