Remote JSON into Table View

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

I have tried this a million different ways and I cant seem to get it to work. I have a page on a server that outputs a JSON dataset. I want to capture that data and put it into a table. it seems like the downloading of the JSON works but I cant figure out how to assign the data to the tabledata variable. What is not clear to me is how to set "tabledata = json;" here is my code which results in this error.

ERROR

[WARN] Exception in event callback. {
    line = 67;
    message = "'undefined' is not an object (evaluating 'tabledata.length')";
    name = TypeError;
    sourceId = 140634112;
}

CODE

function exhibitorsCat(_args) {
    var win = Ti.UI.createWindow({backgroundColor:'#fff'});
 
    var isMobileWeb = Titanium.Platform.osname == 'mobileweb';
 
 
 
//THIS COMMENTED CODE WORKS
    //create table view data object
/*  var tabledata = [
        {title:'Company 1', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'},
        {title:'Company 2', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'},
        {title:'Company 3', category:['Designer Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'},
        {title:'Company 4', category:['Eco Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'},
 
    ];
 
*/
 
 
 
//THIS CODE DOES NOT
   // var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, '/showtime/exhibitors.JSON');
var tabledata;
var url = "http://xxxxxxx.com/exhibitorsJSON.cfm";
var client = Ti.Network.createHTTPClient({
    // function called when the response data is available
    onload : function(e) {
        var json = JSON.parse (this.responseText);
        tabledata = json;
   },
 
   // function called when an error occurs, including a timeout
   onerror : function(e) {
       // need a real error handler here...
       Ti.API.debug(e.error);
       alert('error');
   },
   timeout : 5000  // in milliseconds
});
// Prepare the connection.
client.open("GET", url);
// Send the request.
client.send();
 
 
 
//  Ti.include("/etc/readJSON.js");
 
//  var tabledata = readJSON(exhibitors.JSON)
 
    Ti.include("/etc/filterExhibitors.js");
 
 
 
    if (Titanium.Platform.name == 'iPhone OS')
    {
        Ti.include("/etc/version.js");
 
        if (isiOS4Plus())
        {
 
        }
    }
 
    // create table view
    for (var i = 0; i < tabledata.length; i++ ) { tabledata[i].color = '#000'; tabledata[i].font = {fontWeight:'bold'} };
    var tableview = Titanium.UI.createTableView({
        data: filterExhibitors(tabledata, _args.title), //filter the exhibitors list on the title passed
        style:1
    });
 
    // create table view event listener
    tableview.addEventListener('click', function(e)
    {
        if (e.rowData.test)
        {
            var ExampleWindow = require(e.rowData.test),
                win = new ExampleWindow({title:e.rowData.title,containingTab:self.containingTab,tabGroup:self.tabGroup});
            if (Ti.Platform.name == "android") {
 
            } else {
                win.backgroundColor = "#fff";
                win.barColor = "#111";
                win.barColor='orange';
                win.title=e.row.title;
            }
 
 
            if (e.index == 3)
            {
                if (Ti.Platform.name == "iPhone OS") {
                    win.hideTabBar();
                }
            }
            if (Ti.Platform.name==='android' && e.rowData.test.indexOf('window_properties') >= 0) {
                // As explained in apidoc for Window, if opacity is ever to be changed for an Android
                // activity during its lifetime, it needs to use a translucent background.  We trigger
                // using a translucent theme by the presence of the opacity property, so we need to
                // set it here.  Setting it to 1 means it's totally opaque, but gives us the property to
                // make it more transparent later with the "toggle opacity" test.
                win.backgroundColor = "#191919"
                win.opacity = 1;
            }
            _args.containingTab.open(win,{animated:true});
        }
    });
 
    // add table view to the window
    win.add(tableview);
    return win;
};
 
module.exports = exhibitorsCat;

— asked 8 months ago by David Jones
3 Comments
  • would help to see the JSON output?

    — commented 8 months ago by Aaron Saunders

  • It may not have been the right way but I just put this into a file that I can access on the server

    [
            {title:'Company 1', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'},
            {title:'Company 2', category:['Contract Fabrics'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'},
            {title:'Company 3', category:['Designer Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'},
            {title:'Company 4', category:['Eco Friendly'], hasChild:true, selectedBackgroundColor:'orange', test:'ui/common/showtime/exhibitor'},
     
        ]

    This code will download the contents and display them in an alert box but I dont know how to set the value of tabledata so it can be used out side of the onload event.

    var xhr = Ti.Network.createHTTPClient({
        onload: function(e) {
            // this.responseText holds the raw text return of the message (used for JSON)
            // this.responseXML holds any returned XML (used for SOAP web services)
            // this.responseData holds any returned binary data
            Ti.API.debug(this.responseText);
            alert('success');
            var json = this.responseText;
            alert(json);
     
        },
        onerror: function(e) {
            Ti.API.debug(e.error);
            alert('error');
        },
        timeout:5000
    });
     
    xhr.open("GET", url);
    xhr.send();

    — commented 8 months ago by David Jones

  • i am assuming that the space is a typo... in the onload function, curious what happens when you run this

    onload : function(e) {
        var json = JSON.parse(this.responseText);
        tabledata = json;
        Ti.API.info(this.responseText);
    },

    — commented 8 months ago by Aaron Saunders

1 Answer

i think the problem, is that you set the data for your tableview before the download ist finished, so nothing appears in the tableview. 1. get the json data 2. parse the string in the onload callback 3. also refresh the tableview in the callback

var tabledata = [];
 
var tableView = Ti.UI.createTableView();
 
var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
    // parse the reponse string into json array
    tabledata = JSON.parse(this.responseText;)
        // populate the tableView
    tableView.setData(tabledata);
 
    },
    onerror: function(e) {
        Ti.API.debug(e.error);
        alert('error');
    },
    timeout:5000
});
 
xhr.open("GET", url);
xhr.send();

Your Answer

Think you can help? Login to answer this question!