Can't get the JSON Twitter examples to work
I've tried 3 or 4 different JSON twitter examples and I can't figure out what might be wrong. I even tried 1.3.0 vs 1.4.0 SDK. I am currently using these examples in an iPad app that I am using the 3.2 Apple SDK with.
This is the error in the exception I am getting and it happens on the for loop. What am I doing wrong?
Result of expression 'tweets' [null] is not an object
function loadTweets()
{
// 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://api.twitter.com/1/statuses/user_timeline.json?screen_name=appcelerator');
// Runs the function when the data is ready for us to process
loader.onload = function()
{
try
{
if( this.status == 200 )
{
//also tried this with same result
//var tweets = eval('('+this.responseText+')');
var tweets = JSON.parse( this.responseText );
//Result of expression 'tweets' [null] is not an object
for (var i = 0; i < tweets.length; i++)
{
//never make it in here, but this is the code where the table is built up
}
}
}
catch(E){
alert(E);
}
// 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();
}
loadTweets();
2 Answers
-
Hi Christy,
Please try the code below. It should work for you.
var win = Titanium.UI.currentWindow; function loadTweets() { // 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://api.twitter.com/1/statuses/user_timeline.json?screen_name=appcelerator'); // Runs the function when the data is ready for us to process loader.onload = function() { try { if( this.status == 200 ) { var tweets = eval('('+this.responseText+')'); for (var i = 0; i < tweets.length; i++) { var row = Titanium.UI.createTableViewRow({height:'auto'}); var image = Titanium.UI.createImageView({ image:tweets[i].user.profile_image_url, top:0, left:0, height:48, width:48 }); row.add(image); rowData[i] = row; } } } catch(E){ alert(E); } // 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(); } loadTweets();
-
BirdHouse: Twitter OAuth for Android & iPhone
Intro: This isn't an answer to fix your code, but perhaps using BirdHouse as a work around can be a solution. Or, you can check your code with it to find possible corrections that need made.
Cross-Platform Support & Up-to-Date: BirdHouse has been tested on Android with Linux and Mac as well as on iPhone, both using the latest Titanium Mobile SDK (1.6.2). It has been tested by others and is being maintained to fix any issues.
Go here for the code and go here for the test app.
About: The script can send tweets, retrieve tweets, authenticate, "deauthenticate", and send custom API calls to Twitter. For sending tweets, a custom UI popup appears. It is designed to work similar to Titanium's Facebook module.
David Riccitelli's oauth-adapter doesn't work on Android, but some have forked it to work, such as Ketan Majmudar (go here for his code), I have also tried other OAuth scripts without any luck. I haven't been able to get anything to work, so I developed BirdHouse. Let me know if you have any issues or questions, I'd be glad to help.