get data from a nonlocal JSON

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

Hey,

I want to fill a tableview with data from a json file. With the local json file everything works fine but i don't really get how to do the same thing with the json file being "on the web". I know this sis possible but i dont know how to do this. Could you please help me?

btw: I want to do this because my app has constantly filled with new content (adding new rows to the tableView) but I cant update the entire app everytime I add another row. if there is any other way to do this, please tell me.

My code is here:

var win3 = Titanium.UI.createWindow({  
    title:'Tipps & Tricks',
    backgroundImage:'screennormal5.png',
});
 
win1.orientationModes = [Ti.UI.PORTRAIT];
 
var tab3 = Titanium.UI.createTab({  
    icon:'tipps30x30.png',
    title:'Tipps & Tricks',
    opacity: 0.7,
    window:win3
});
 
var file = Ti.Filesystem.getFile("test2.js");
 
var listArray = JSON.parse(file.read().text);
 
var tablescroll = Ti.UI.createScrollView({
    height: 'auto',
});
 
win3.add(tablescroll)
 
var TheTable = Titanium.UI.createTableView({
    top:30,
    left:20,
    right:15,
    bottom:25,
    height:'796',
    font: { fontSize:16, fontFamily:'Helvetica', fontWeight: 'bold' },
    borderRadius:10,
    backgroundColor:'#F0F9FB',
    borderColor:'#309fbc',
    borderWidth: 3,
    backgroundSelectedColor:'white',
    separatorColor:'#309fbc',
    data:listArray
});
 
tablescroll.add(TheTable );
 
 
tabGroup.addTab(tab3);  
 
tabGroup.open();

1 Answer

Use the HTTPClient to retrieve your data. You'll define an onload function that is called when the HTTPClient has received a response.

In that function, parse the JSON. You can build the listArray and call setData() on your tableview.

Put this right after you add TheTable:

var url = "http://my.url.here";
var client = Ti.Network.createHTTPClient({
    // function called when the response data is available
    onload : function(e) {
        var listArray = JSON.parse (this.responseText);
        TheTable.setData (listArray);
   },
 
   // 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();
BTW -- you don't want to add a TableView to a ScrollView. See this thread for lots of details.

Your Answer

Think you can help? Login to answer this question!