我正在努力将我的项目从AFNetworking转移到Alamofire.真的很喜欢这个项目.POST请求工作正常,但是,我在尝试发出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) }
我收到此错误: -1005 The network connection was lost
但是,如果我将请求类型更改为.POST
,则请求"正常".我收到401代码,但至少请求不会丢失网络连接.
我究竟做错了什么?
您将参数编码为请求正文中的JSON,尝试通过将编码更改为URL
:来编码URL中的参数:
return Alamofire.request(.GET, urlString, parameters: parameters, encoding: .URL, headers: headers)
由于这是默认行为,您只需删除它:
return Alamofire.request(.GET, urlString, parameters: parameters, headers: headers)