我试图通过以下代码访问字典API:
func apiConnection() {
if let url = NSURL(string:"https://wordsapiv1.p.mashape.com/words/soliloquy") {
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "GET"
request.addValue("aaabbbccc", forHTTPHeaderField: "X-Mashape-Key")
request.addValue("application/json", forHTTPHeaderField: "Accept")
print(request.HTTPBody)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil {
print("\(error)")
return
}
print("\(response)")
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("\(responseString)")
}
}
}
我是否对代码做错了什么?对不起,我在这方面很新。我使用的API来自{{3}}
答案 0 :(得分:1)
您配置了任务,但没有启动它
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
...
}
task.resume() // Start the task