Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Using the Wordpress JSON API on Titanium Mobile

Using the Twitter.js from the KitchenSink, I am trying to get it to work with the Wordpress JSON API.

http://wordpress.org/extend/plugins/json-api/other_notes/

But tweaking the xhr.onload func didn't go easy as I had thought it would.

I have set the new variables for the Wordpress like below,

    xhr.onload = function()
    {
        try
        {
            // var tweets = eval('('+this.responseText+')');
            var wps = eval('('+this.responseText+')');

            //for (var c=0;c<tweets.length;c++){
            for (var c=0;c<wps.length;c++){

                // var tweet = tweets[c].text;
                var wp = wps[c].posts.content;

                // var user = tweets[c].user.screen_name;
                var user = wps[c].posts.author.name;
.
.
.

But getting no responseText from the JSON API.

The default reponse from the Wordpress JSON API goes like this;

{
  "status": "ok",
  "count": 1,
  "count_total": 1,
  "pages": 1,
  "posts": [
    {
      "id": 1,
      "type": "post",
      "slug": "hello-world",
      "url": "http:\/\/localhost\/wordpress\/?p=1",
      "title": "Hello world!",
      "title_plain": "Hello world!",
      "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!<\/p>\n",
      "excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\n",
      "date": "2009-11-11 12:50:19",
      "modified": "2009-11-11 12:50:19",
      "categories": [],
      "tags": [],
      "author": {
        "id": 1,
        "slug": "admin",
        "name": "admin",
        "first_name": "",
        "last_name": "",
        "nickname": "",
        "url": "",
        "description": ""
      },
      "comments": [
        {
          "id": 1,
          "name": "Mr WordPress",
          "url": "http:\/\/wordpress.org\/",
          "date": "2009-11-11 12:50:19",
          "content": "<p>Hi, this is a comment.<br \/>To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.<\/p>\n",
          "parent": 0
        }
      ],
      "comment_count": 1,
      "comment_status": "open"
    }
  ]
}
— asked August 12th 2010 by Jae Lee
  • api
  • json
  • wordpress
0 Comments

4 Answers

  • Hello, I think you shoul use the JSON object to work with JSON objects. Like this:

    xhr.onload = function() {
        var response = JSON.parse(this.responseText);
    };
    

    If you want to send an answer, you should use JSON.stringify():

    var answ = JSON.stringify({
        answer: "Answer"
    });
    
    — answered August 12th 2010 by Adam Wallner
    permalink
    1 Comment
    • Thanks for your tip. I have tried parsing the JSON object by going JSON.parse instead of eval, but still getting nothing back.

      — commented August 12th 2010 by Jae Lee
  • Hi Jae, did you ever have any luck with this?

    — answered September 2nd 2011 by Craig Snodgrass
    permalink
    0 Comments
  • try this :) but i'm still cant parse image on it :)

    loader.onload = function() 
        {
            var cats = eval('('+this.responseText+')');
    
            for (var i = 0; i < cats.count; i++)
    
    
            {
    
    
                var cat = cats.posts[i].title; // The profile image
                var judul = cats.posts[i].excerpt; // The profile image
                // Create a row and set its height to auto
                var row = Titanium.UI.createTableViewRow({height:'auto'});
    
                // Create the view that will contain the text and avatar
                var post_view = Titanium.UI.createView({
                    height:'auto', 
                    layout:'vertical',
                    top:5,
                    right:5,
                    bottom:5,
                    left:5
                });
    var id_label = Titanium.UI.createLabel({
                    text:cat,
                    left:55,
                    top:10,
                    bottom:5,
                    height:'auto',
                    width:236,
                    textAlign:'left',
                    font:{fontSize:14}
                });
                post_view.add(id_label);
    
    var tweet_lbl = Titanium.UI.createLabel({
                    text:judul,
                    left:55,
                    top:0,
                    bottom:10,
                    height:'auto',
                    width:236,
                    textAlign:'left',
                    font:{fontSize:10}
                });
                post_view.add(tweet_lbl);
                // Add the post view to the row
                row.add(post_view);
                // Give each row a class name
                row.className = "item"+i;
                // Add row to the rowData array
                rowData[i] = row;
            var tableView = Titanium.UI.createTableView({data:rowData}); 
    
            post_view.addEventListener('click', function(e){
            alert('jancuk'); 
        }); 
    
            }
    
            // Create the table view and set its data source to "rowData" array
    
            //Add the table view to the window
            win.add(tableView);
    
        };
        // Send the HTTP request
        loader.send();
    }
    loadTweets();
    
    — answered February 3rd 2012 by Arie Prasetyo
    permalink
    2 Comments
    • Thanks for this response. I'll be looking into how images can be retrieved.

      Cheers.

      — commented February 23rd 2012 by Christiaan de Rijk
    • Did you guys ever work this out? Still looking for the cleanest way to user WP API to generate a table of posts. Thanks!

      — commented November 5th 2012 by Joseph Lancaster
  • Sorry, this is a little behind the ball, but this is working for me. Hope it can help someone else.

        xhr.onload = function() {    
            try {
                var stories = JSON.parse(this.responseText);
                Ti.API.info(stories);         
                for (var i = 0; i < stories.length; i++){
                var headline = stories.posts[i].title;
                Ti.API.info(headline);
            }
            catch(E)
            {
                alert(E);
            }
        };
    xhr.send();
    
    — answered July 18th 2012 by Tim Shedor
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.