【问题标题】:Http get with query parameters in Swift-2Swift-2 中带有查询参数的 Http 获取
【发布时间】:2015-11-19 08:06:19
【问题描述】:

了解如何在 Swift2 中使用查询字符串进行 http get 调用

我可以看到很多 Objective-c 和旧版本 swift 的示例,但找不到 Swift2

【问题讨论】:

    标签: ios swift swift2 xcode7


    【解决方案1】:

    这是一个在 iTunes API 上执行搜索的示例:

    let timeout = 11 as NSTimeInterval
    let searchTerm = "philip+glass"
    let url = NSURL(string: "https://itunes.apple.com/search?term=\(searchTerm)")
    let request: NSURLRequest = NSURLRequest(URL: url!,
                                            cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringCacheData,
                                            timeoutInterval: timeout)
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)
    let task: NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {
        (data, response, error) in
            if response == nil {
                print("Timeout")
            } else {
                print(String(data: data!, encoding: NSUTF8StringEncoding))
            }
        }
    )
    
    task.resume()
    

    【讨论】:

    • 谢谢,但是说我想设置多个名称值参数并用适当的编码构造它们,是否有一些函数可以轻松构造这些参数?
    • stackoverflow.com/questions/27723912/… 的答案有一些扩展可以为你完成这些事情。
    猜你喜欢
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2013-03-11
    • 2023-04-03
    相关资源
    最近更新 更多