1

I have a problem when I tried to GET data from a web api. But what i get is nil. I couldn't get the article list from the api. Any advice?

api doc address: yazar.io/doc/

My NSObject File:

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)
        }
    }

My table view controller;

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
        }
    }

}
11
  • Welcome to Stack Overflow, please read this article on how to properly ask a question. You could improve your question a lot by reducing the amount of code to the code that is problematic and by stating what you’ve already tried to solve the problem. Commented Mar 24, 2015 at 12:59
  • Thanks. My Alamofire.request returns nil. I couldn't find a way to fix it. Commented Mar 24, 2015 at 13:14
  • Debug. Try to use a println just before you send the request, print the URL that you try to access and paste that url in your web browser, do you get any data at all. Check if you get any data. Post the error that you get. Just a few simple tips on how to help yourself get a better answer from the community Commented Mar 24, 2015 at 13:28
  • println(urlString) returns correct url: yazar.io/api/article/feed/1 -->correct println(page) returns : 1 -->correct println(articles[]) returns nil. It seems that i couldnt parse the data correctly Commented Mar 24, 2015 at 13:53
  • Could you println your articles in your loadarticles function for me. Just after you set self.articles Commented Mar 24, 2015 at 15:02

2 Answers 2

1

This link http://yazar.io/api/newspaper/1 gives a 404 error, so obviously you will get a nil returned from the request.

Sign up to request clarification or add additional context in comments.

Comments

0

You are using the baseURL in the request

 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)
        }
    }

change the baseURL to urlString like this

 Alamofire.request(.GET, urlString, parameters: parameters).responseJSON{(_, _, data, _) -> Void in
        let articles = JSON(data ?? [])
        response(articles)
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.