我正在努力将我的项目从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&#34;网络连接是 丢失&#34。 UserInfo = {NSUnderlyingError = 0x14e9c77f0 {错误 Domain = kCFErrorDomainCFNetwork Code = -1005&#34;(null)&#34; 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一起运行良好。
这里是调试印刷(请求)结果(它是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有关吗?
答案 0 :(得分:10)
解决!我正在使用JSON编码进行GET请求。这是答案:Alamofire Request error only on GET requests
答案 1 :(得分:-2)
如果您在vc或其他对象中保留请求,则此错误将消失。