当我尝试从web api获取数据时,我遇到了问题。但我得到的是零。 我无法从api获得文章列表。有什么建议吗?
api doc address:yazar.io/doc /
我的NSObject文件:
import Alamofire
struct YazarIoService {
// Doc http://yazar.io/doc/
private static let baseURL = "http://yazar.io"
private enum ResourcePath: Printable {
case Articles
case StoryUpvote(storyId: Int)
var description: String {
switch self {
case .Articles: return "/api/article/feed/1"
case .StoryUpvote(let id): return "/api/client/favorites/action/"
}
}
}
static func articleForSection(section: String, page: Int, response: (JSON) -> ()){
let urlString = baseURL + ResourcePath.Articles.description
println(urlString)
let parameters = [
"page": toString(page)
]
println(parameters)
Alamofire.request(.GET, baseURL, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}
}
我的表格视图控制器;
class articlesTableViewController: UITableViewController, MakaleTableViewCellDelegate {
let transitionManager = TransitionManager()
var articles: JSON! = []
func loadarticles(section: String, page: Int){
YazarIoService.articleForSection(section, page: page) { (JSON) -> () in
self.articles = JSON["articles"]
self.tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().setStatusBarStyle(UIStatusBarStyle.LightContent, animated: true)
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
loadarticles("", page: 1)
println(articles[])
}
@IBAction func MenuButtonDidTouch(sender: AnyObject) {
performSegueWithIdentifier("MenuSegue", sender: self)
}
@IBAction func LoginbutonDidTouch(sender: AnyObject) {
performSegueWithIdentifier("LoginSegue", sender: self)
}
//number of rows
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return articles.count //
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("MakaleCell") as MakaleTableViewCell //--> cell'in özelliklerine erişmek içim bunu yazdık. Eskiden UITableViewCell'di. Değiştirmezsen cell.title... vs çalışmakz.
let makale = articles[indexPath.row]
cell.configureWithMakale(makale)
cell.delegate = self
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
performSegueWithIdentifier("WebSegue", sender: indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
// MARK: MakaleTableViewCellDelegate
func makaleTableViewCellDidTouchUpvote(cell: MakaleTableViewCell, sender: AnyObject) {
// TODO: Implement Upvote
}
func makaleTableViewCellDidTouchComment(cell: MakaleTableViewCell, sender: AnyObject) {
performSegueWithIdentifier("CommentsSegue", sender: self)
}
// MARK: Misc
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "WebSegue" {
let toView = segue.destinationViewController as WebViewController
let indexPath = sender as NSIndexPath
let url = articles[indexPath.row]["article_url"].string!
toView.url = url
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)
toView.transitioningDelegate = transitionManager
}
}
}
答案 0 :(得分:1)
这个链接http://yazar.io/api/newspaper/1给出了404错误,所以显然你会从请求中返回一个nil。
答案 1 :(得分:0)
您在请求中使用了baseURL
static func articleForSection(section: String, page: Int, response: (JSON) -> ()){
let urlString = baseURL + ResourcePath.Articles.description
println(urlString)
let parameters = [
"page": toString(page)
]
println(parameters)
Alamofire.request(.GET, baseURL, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}
}
将baseURL更改为urlString,如下所示
Alamofire.request(.GET, urlString, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
let articles = JSON(data ?? [])
response(articles)
}