好吧,我将JSON响应设置为nil
,但为什么我得到The data couldn’t be read because it isn’t in the correct format.
我已经设置了这样的表格视图:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if (section==0){
return 1
}else{
if(self.listHistory == nil){
return 1
}else{
if(self.listHistory.count==0){
return 1
}else{
return self.listHistory.count
}
}
}
}
以下是我提出请求的方式:
let apikey = "56c4382d6873f46e7d00000133c2ca8a8220434a4ed619a435f392a8"
let request = NSMutableURLRequest(URL: NSURL(string: "https://api.123123.biz/123/NonTransactionManager/TransactionHistory")!)
request.setValue(apikey, forHTTPHeaderField: "APIKey")
request.HTTPMethod = "POST"
ZProgressHUD.show("Please Wait")
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in
let postString = ("msisdn=\(self.msisdn!)¤tPage=0&pageSize=5&accountID=6&startDate=2016-01-01&endDate=\(tanggal)")
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else {
ZProgressHUD.dismiss()
print("error=\(error)")
return
}
do {
let response = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary
dispatch_async(dispatch_get_main_queue(), {
ZProgressHUD.dismiss()
self.listHistory = response?.objectForKey("accountHistoryDetails") as! NSArray
self.tableView.reloadData()
})
}catch let error as NSError {
print(error.localizedDescription)
}
}
task.resume()
}
你能告诉我我该怎么做才能摆脱这个错误吗?
答案 0 :(得分:0)
尝试以下解决方案:
您需要传递内容类型:
request.addValue(" application / json",forHTTPHeaderField:" Content-Type") request.addValue(" application / json",forHTTPHeaderField:" Accept")
let parseJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary