createHTTPClient onload called multiple times

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

Hi,

I am making requests to Instagram to pass me back the users recent photo's. This is my code:

function fetchData(pictureView, mainView)
{
 
    var userid = '';   
    userid = apiData.url.substring(apiData.url.indexOf('access_token='));
    userid = userid.substring(userid.indexOf('='));
    myArray = userid.split('.');
    userid = myArray[0].substr(1);
    Ti.API.info(userid);
    var urlToSend = apiData.request_url + 'users/' + userid + '/media/recent/?access_token=' + apiData.access_token;    
 
    var views = [];
 
    var xhr = Ti.Network.createHTTPClient({
        onload: function(e) {
            // this just gets the first image, need to loop around the data array - TODO
            var parsedData = JSON.parse(this.responseText);
            var url = parsedData.data[1].images.standard_resolution.url;  // holds image
 
            for (var i = 0; i < 1; i++) {  // this needs changing to amount of images in JSON
                var instagramImage = Ti.UI.createImageView({
                    image: url, //'/images/Tshirt Style/' + images[i],
                    top: 0, left: 0,
                    width: 'auto',
                    height: 'auto',
 
                });
                views.push(instagramImage);    
            }           
            var scrollview = Ti.UI.createScrollableView({
                views: views, width: '200dp',
                top: '100dp', right: '60dp', bottom: '170dp', left: '60dp',showPagingControl:false,pagingControlColor:'black'
            });
            mainView.add(scrollview);
            return;
 
        },
        onerror: function(e) {
            Ti.API.debug(e.error);
            alert('Timed out');
        },
        timeout:10000
    });
 
    xhr.open("GET", urlToSend);
    xhr.send(); 
}
The parsedData is the JSON returned back from Instagram, but, this seems to get returned from one to up to four times, thus onload being called more than once, can anybody see the reason? What I'm trying to do, is put the images url from the JSON returned from Instagram into the scrollview.

Hope this makes sense?!

Regards, Steve

— asked 8 months ago by steve green
2 Comments
  • you are going to need to provide more code... specifically how is fetchData being called?

    — commented 8 months ago by Aaron Saunders

  • Hi,

    Thanks, yes, I think it is the code that calls fetchData that is the issue, this is it:

    localWebview = Ti.UI.createWebView({
            top:'50dp',
            left:10,
            right:10,
            width:1,
            height:1,
            backgroundColor:'transparent',
            touchEnabled:true,
            url : apiData.authorize_url + '?client_id=' + apiData.client_id + '&redirect_uri=' + apiData.redirect_uri + '&response_type=token&display=touch',
            //autoDetect : [Ti.UI.iOS.AUTODETECT_NONE]
           });
        localWebview.hide(); 
     
        localWebview.addEventListener('load', function(e) {
            if (e.url.indexOf('access_token=') != -1) {
                apiData.url = e.url;
                localWebview.stopLoading();
                saveToken(e.url.substring(e.url.indexOf('access_token=') + 13));
                Ti.App.fireEvent('app:instagram_integrated');
                // Go fetch the data now we are authorised and have access_token
                fetchData(pictureView, view);
     
                Ti.API.info("FINISHED");
     
            }
        });
    The FINISHED is displayed multiple times, thus it looks like the eventlistener for load is being called multiple times?

    Thanks, Steve

    — commented 8 months ago by steve green

1 Answer

Accepted Answer

you need to stop the webView from loading as soon as you get the access token.

what does stop loading do? It needs to remove the eventListener

— answered 8 months ago by Aaron Saunders
answer permalink
7 Comments
  • I am stopping the loading as soon as I get the token, this is what stoploading is meant to do. How do I remove the eventListener? Something like removeEventListener('load', <--- what goes here?);

    Regards, Steve

    — commented 8 months ago by steve green

  • I just posted the API I wrote... take a look at it

    Instagram API

    — commented 8 months ago by Aaron Saunders

  • Thanks for this :-) Very nice. So, could I just do a GET instead of using a webview to get the access_token? ;-)

    — commented 8 months ago by steve green

  • Show 4 more comments

Your Answer

Think you can help? Login to answer this question!