检查时服务工作正常: http://lyrics.wikia.com/api.php?func=getSong&artist=Tom+Waits&song=new+coat+of+paint&fmt=json
但是当我在Swift中尝试它时,我的数据读取“233字节”。
任何人都知道出了什么问题?
这是我的代码:
func getWikiaInfo(completionHandler: @escaping (_ success: Bool?, _ error: NSError?) -> Void) {
let url = URL(string: "http://lyrics.wikia.com/api.php?func=getSong&artist=Tom+Waits&song=new+coat+of+paint&fmt=json")!
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
// no error, woohoo!
if error == nil {
// there was data returned
if let data = data {
let parsedResult: [String:AnyObject]!
do {
parsedResult = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:AnyObject]
} catch {
print("Could not parse the data as JSON: '\(data)'")
return
}
print(parsedResult)
completionHandler(true, nil)
}
}
}