XHR Request Error?

You must Login before you can answer or comment on any questions.

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);
};

— asked 11 months ago by Graham Jeffrey
1 Comment
  • Can you give us details on what is the error that you get? Try to console try the error message. This will probably help you understand or us help you out in rectifying the issue

    — commented 11 months ago by Abishek R Srikaanth

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.parse rather 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();

— answered 11 months ago by Malcolm Hollingsworth
answer permalink
7 Comments
  • thanks for your help very much , now i will make a slide show for texts, thanks again, i have read all your notes and will pay attention to them, thanks again

    — commented 11 months ago by Graham Jeffrey

  • Malcolm i was trying to make next tweet with setTimeOut function but it gives error, i am using this code with m slideshow but why didn't it works with this?

    var tweet_text = Ti.UI.createLabel({
            backgroundColor: 'orange',
            text: '',
            color: '#000',
            top:50,
            left:0,
            width:320,
            height:100
        });
     
        win.add(tweet_text);
     
    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);
            tweet_text.text = "test"+feeds[0];
        }
     
        goNextTweet = function(){
        intTweet = 0;
        Ti.API.info("Total:"+intTweets);    
     
        intTweet = intTweet >= intTweets ? 0 : intTweet;
        intTweet ++;
        var intTweet = feeds[intTweet];
        tweet_text.text = feeds[intTweet];
        return intTweet;
     
        }
        setTimeout(function() {
        loadTwitter(goNextTweet());
     
        }, 3000);
     
    };
    and i want to ask you a question about the twitter loader function, why did you write
    function loadTwitter(tweets)
    why this isn't
    function loadTwitter()
    i want to learn the different between them, if you are ok thanks

    — commented 11 months ago by Graham Jeffrey

  • Hi

    I will check and tweak your code in the morning.

    The reason I passed the tweets in a parameter was to simplify your previous code, as you had re-parsed the tweets inside the function you called after you had already parsed them.

    — commented 11 months ago by Malcolm Hollingsworth

  • Show 4 more comments

Your Answer

Think you can help? Login to answer this question!