hello, i am using the code below, but it gives a connection error? i am using the same code for my CoverField page but not giving an error?
var win = Ti.UI.currentWindow; win.backgroundColor = '#FFF'; var feeds = []; var xhr_request = Titanium.Network.createHTTPClient(); xhr_request.setTimeout(5000); xhr_request.onerror = function(e){ alert('Check Your Internet Connection'); } xhr_request.open("GET", 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=twitter_usename_here&include_rts=true'); xhr_request.onload = function(){ try{ var tweets = eval('('+this.responseText+')'); Ti.API.info("tweet_text:"+feeds); loadTwitter(); }catch(e){ alert('Check Your Internet Connection'); } } xhr_request.send( ); function loadTwitter( ){ var tweets = eval('('+this.responseText+')'); for (var i = 0; i < tweets.length; i++) Ti.API.info("Length:"+tweets.length); { tweets = tweets[i].text; Ti.API.info(tweets); feeds.push(tweets); } var tweet_text = Ti.UI.createLabel({ text:images, color:'#000', top:50, left:0, width:320, height:100 }); win.add(tweet_text); };
1 Answer
Accepted Answer
Hi Gurkan
I am not sure best to explain what went wrong here.
- You set all events for xhr before you try calling the open/send routines.
- You did not enter a 'screen name' in the URL
- You added images to the label tweet_text, except you did not have a variable called images
- You should use
JSON.parserather than eval, it is easier and safer. - You duplicated the eval on the responseText.
I have made your code work and simplified it a bit.
var win = Titanium.UI.createWindow({ title : 'Win 1', backgroundColor : '#fff' }); var strScreenName = "microsoft"; function loadTwitter(tweets) { Ti.API.info("Length:" + tweets.length); var feeds = [], intTweet = 0, intTweets = tweets.length; for (intTweet = 0; intTweet < intTweets; intTweet = intTweet + 1) { feeds.push(tweets[intTweet].text); } var tweet_text = Ti.UI.createLabel({ backgroundColor: 'orange', text: feeds[0], color: '#000', top:50, left:0, width:320, height:100 }); win.add(tweet_text); Ti.API.info("Length:" + tweets[0].text); }; var xhr_request = Titanium.Network.createHTTPClient(); xhr_request.setTimeout(5000); xhr_request.onerror = function(e){ alert('Check Your Internet Connection'); } xhr_request.onload = function() { var response = this.responseText; var tweets = JSON.parse(response); loadTwitter(tweets); } xhr_request.open("GET", 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' + strScreenName + '&include_rts=true'); xhr_request.send( ); win.open();
Your Answer
Think you can help? Login to answer this question!