Cod*_*ton 10 rest xcode ios swift alamofire
我正在努力将我的项目从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)
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的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)")
}
}
Run Code Online (Sandbox Code Playgroud)
这是我遇到的错误:
错误域= 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"
Run Code Online (Sandbox Code Playgroud)
我必须将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"
Run Code Online (Sandbox Code Playgroud)
这可能是GET与POST的关系吗?
| 归档时间: |
|
| 查看次数: |
8830 次 |
| 最近记录: |