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"
}
]
}
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" });
-
Hi Jae, did you ever have any luck with this?
-
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();
-
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();