2017-03-09 70 views
1
錯誤代碼

我使用的是Alamofire 4.當我做試圖訪問Alamofire

print(response.debugDescription) 

我在控制檯是這樣的:

[Request]: https://api2.website.com 
[Response]: nil 
[Data]: 0 bytes 
[Result]: FAILURE: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={NSUnderlyingError=0x17444ace0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x170490e50 [0x1ab165bb8]>{length = 16, capacity = 16, bytes = 0x100201bb341d1f890000000000000000}, _kCFStreamErrorCodeKey=57, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=https://api2.flowwow.com/api2/client/info/?auth_token=da88d8aa49ff6f8bb4e1&hash=7f38be3f68db39a6d88687505fdb9ba5&partner_id=1004, NSErrorFailingURLKey=https://api2.website.com, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=57, NSLocalizedDescription=The Internet connection appears to be offline.} 
[Timeline]: Timeline: { "Request Start Time": 510763454.078, "Initial Response Time": 510763455.293, "Request Completed Time": 510763455.293, "Serialization Completed Time": 510763455.297, "Latency": 1.215 secs, "Request Duration": 1.215 secs, "Serialization Duration": 0.005 secs, "Total Duration": 1.220 secs } 

而且還有一個特別的線,我感興趣:

Error Domain=NSURLErrorDomain Code=-1009 

我怎樣才能得到這個代碼,所以我能正確處理錯誤。我嘗試了所有可以組合的組合,但這裏沒有任何代碼的痕跡。

+0

我認爲[這個問題/答案](http://stackoverflow.com/questions/29131253/swift-alamofire-how-to-get-the-http-response - 狀態碼)應該是有用的,或者[這一個](http://stackoverflow.com/questions/36331234/could-not-get-the-server-error-message-from-alamofire-3-3- 0)。 –

+0

@AhmadF是的,在發佈之前我已經檢查過這些 - 它沒有幫助。 – Edward

回答

2

當您使用Alamofire撥打電話時,它會返回一個響應,您可以在其中檢查是否有任何錯誤。這是Alamofire處理錯誤的簡單例子。

Alamofire.request("https://your.url.com").responseJSON { response in 
    if (response.result.isSuccess){ 
     //do your json stuff 
    } else if (response.result.isFailure) { 
     //Manager your error 
     switch (response.error!._code){ 
      case NSURLErrorTimedOut: 
       //Manager your time out error 
       break 
      case NSURLErrorNotConnectedToInternet: 
       //Manager your not connected to internet error 
       break 
      default: 
       //manager your default case 
      } 
    } 
} 

享受:)