【发布时间】:2019-07-04 13:25:16
【问题描述】:
尝试通过spotify web api 搜索艺术家的曲目。但是Spotify 已经更新了它的 api,现在需要一个 Token 来访问它。我必须标记,但找不到任何关于如何通过 Alamofire 使用它的方法。这是代码。任何帮助将不胜感激,或指出正确的方向。
class TableViewController: UITableViewController {
var names = [String]()
var searchURL = "https://api.spotify.com/v1/search?q=Odesza&type=track"
typealias JSONStandard = [String : AnyObject]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
callAlamo(url: searchURL)
}
func callAlamo(url : String){
AF.request(url).responseJSON(completionHandler: {
response in
self.parseData(JSONData: response.data!)
})
}
func parseData(JSONData : Data) {
do {
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
print(readableJSON)
if let tracks = readableJSON["tracks"] as? JSONStandard{
// print(readableJSON)
if let items = tracks["items"] as? NSArray{
for i in 0..<items.count{
let item = items[i] as! JSONStandard
let name = item["name"] as! String
names.append(name)
self.tableView.reloadData()
}
}
}
}
catch{
print(error)
}
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return names.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
cell?.textLabel?.text = names[indexPath.row]
return cell!
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
【问题讨论】: