我正在使用Swift编写应用程序并尝试集成Uber 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

编辑:

我下载了Charles,它显示了以下屏幕:

ios - 错误域= NSURLErrorDomain代码= 1005对Uber-api的 curl 请求-LMLPHP

困扰我的是说"SSL Proxying not enabled for this host: enable in Proxy Settings, SSL locations"的行另外,通过终端输入curl命令时,我无法在Charles上触发任何操作。我想这很正常。

SO上的每个解决方案都告诉我重新启动模拟器,而我确实这样做了。我去了模拟器设置并启用了HTTP服务。我吹了墨盒,然后转到Google的第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 {

    }

并将参数添加到NSURL(例如,使用NSURLComponents)应该可以解决此问题。

09-10 05:26
查看更多