错误域= NSURLErrorDomain代码= -1005卷曲请求到优步api
问题描述:
我正在写一个应用程序使用swift并试图整合优步api。 我想做到这一点确切简单的(!?)要求我做终端:错误域= NSURLErrorDomain代码= -1005卷曲请求到优步api
curl -X GET -G 'https://sandbox-api.uber.com/v1/products' -d server_token=*************************************** -d latitude=21.3088619 -d longitude=-157.8086674
为此我尝试下面的代码:
func performPostUberRequest() {
let url: NSURL = NSURL(string: "https://sandbox-api.uber.com/v1/products")
print("performPostUberRequest Ignited")
let params:[String: AnyObject] = ["server_token" : "***************************************", "latitude" : "21.3088621", "longitude" : "-157.8086632"]
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
do {
try request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
} catch {
}
print("performPostUberRequest ongoing")
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
response, data, error in
if let error = error {
print("error: ")
print(error)
print("data: ")
print(data)
print("response: ")
print(response)
} else if data != nil {
do {
let json: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as! NSDictionary
print("here comes JASON: ")
print(json)
} catch {
print("Epic Fail")
}
}
}
}
但不管我尝试3- 4天现在给我确切的同样的答案:
performPostUberRequest Ignited
performPostUberRequest ongoing
error:
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/products, _kCFStreamErrorCodeKey=-4, NSErrorFailingURLKey=https://sandbox-api.uber.com/v1/products, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x16da79f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=-4, NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/products, NSErrorFailingURLKey=https://sandbox-api.uber.com/v1/products, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=4}}}
data:
nil
response:
nil
编辑:
我下载了查尔斯,这给了我下面的屏幕:
让我困扰的是该行说"SSL Proxying not enabled for this host: enable in Proxy Settings, SSL locations"
另外,我是不是能够通过终端进入curl命令时触发查尔斯什么。我想这是正常的。
SO上的每个解决方案都告诉我要重新启动模拟器,我这样做。我去了模拟器设置并启用了HTTP服务。我吹了墨盒,然后走到谷歌的第2页。请。帮帮我。我。
编辑2:解
下面是最终代码,这要归功于@faarwa
func performPostUberRequest() {
var components = NSURLComponents()
components.scheme = "https"
components.host = "sandbox-api.uber.com"
components.path = "/v1/products"
components.queryItems = [
NSURLQueryItem(name: "server_token", value: "********"),
NSURLQueryItem(name: "latitude", value: "21.3088621"),
NSURLQueryItem(name: "longitude", value: "-157.8086632")
]
let request: NSMutableURLRequest = NSMutableURLRequest(URL: components.URL!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
print("thanks faarwa")
}
答
通过URL而不是HTTP消息体,因为它是一个GET请求发送查询参数。删除:
do {
try request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
} catch {
}
并添加PARAMS到NSURL
应该解决这个问题(例如使用NSURLComponents
)。
很高兴你的工作。干杯! – faarwa