因此,我一直在到处搜索以获取 Titanium 应用程序以从 wordpress 网站读取 JSON,但出现错误:
“未捕获的 SyntaxError:意外令牌)”
不知道如何解决
但这是我的代码
应用程序.js:
// Creates a tab group with Titanium.UI API.
var tabGroup = Titanium.UI.createTabGroup();
// Create the window "mainWin"
var mainWin = Titanium.UI.createWindow ({
title: "Most Recent Blog Posts", // Set the title
backgroundColor: "#fff", // Set the background color to white
url: "wordpress.js" // Link to file which will handle the code for the window
});
// Create the tab "mainTab"
var mainTab = Titanium.UI.createTab ({
title: "Posts", // Title of the tab: "SUYS"
window: mainWin // We will create the window "mainWin"
});
// Add the tab to our tab group
tabGroup.addTab(mainTab);
tabGroup.open();
wordpress.js
// Create variable "win" to refer to current window
var win = Titanium.UI.currentWindow;
// Function loadTweets()
function loadMessage()
{
// Empty array "rowData" for our tableview
var rowData = [];
// Create our HTTP Client and name it "loader"
var loader = Titanium.Network.createHTTPClient();
// Sets the HTTP request method, and the URL to get data from
loader.open("GET","http://http://localhost/wordpress/?json=get_posts");
// Runs the function when the data is ready for us to process
loader.onload = function()
{
var message = eval('('+this.responseText+')');
alert('loaded');
for (var i = 0; i < message.posts.length; i++)
{
console.log(message.posts[i].post_title);
var tweet = message.posts[i].post_content; // The tweet message
var user = message.posts[i].author.name; // The screen name of the user
var avatar = message.posts[i].user_avatar; // 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
});
// Create image view to hold profile pic
var av_image = Titanium.UI.createImageView({
url:avatar, // the image for the image view
top:0,
left:0,
height:48,
width:48
});
post_view.add(av_image);
// Create the label to hold the screen name
var user_lbl = Titanium.UI.createLabel({
text:user,
left:54,
width:120,
top:-48,
bottom:2,
height:16,
textAlign:'left',
color:'#444444',
font:{fontFamily:'Trebuchet MS',fontSize:14,fontWeight:'bold'}
});
post_view.add(user_lbl);
// Create the label to hold the tweet message
var tweet_lbl = Titanium.UI.createLabel({
text:tweet,
left:54,
top:0,
bottom:2,
height:'auto',
width: 'auto',
textAlign:'left',
font:{fontSize:14}
});
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;
}
// Create the table view and set its data source to "rowData" array
var tableView = Titanium.UI.createTableView({data:rowData});
//Add the table view to the window
win.add(tableView);
};
// Send the HTTP request
loader.send();
}
loadMessage();
任何帮助将非常感激!!!