Hi GEEKZ :) please help me with the below code...
-What i need is to load the JSON data when the apps load -Replace the Label1 with JSON data
var win = Titanium.UI.currentWindow; var scrollView = Titanium.UI.createScrollView({ contentWidth:'auto', contentHeight:'auto', top:0, showVerticalScrollIndicator:true, showHorizontalScrollIndicator:true }); var viewAll = Titanium.UI.createView({ width:"100%", height: 370, backgroundColor:"red", top:0 }); var viewUpper = Titanium.UI.createView({ width:"100%", height: 100, backgroundColor:"green", top:0 }); var viewLower = Titanium.UI.createView({ width:"100%", height: "100%", backgroundColor:"yellow", top:100 }); var label1 = Titanium.UI.createLabel({ text:"hi all" }); var json; var response; var GET_DATA = Titanium.Network.createHTTPClient({ onload: function(){ alert("hi"), GET_DATA.open("POST","http://172.16.1.215/sites/iOS1/get_main.php"), GET_DATA.send(), json = this.responseText; response = JSON.parse(json); if (response.info) { alert(response.info); } else { alert(response.message); } } }); //win.add(upperWin); viewLower.add(label1); viewAll.add(viewUpper); viewAll.add(viewLower); scrollView.add(viewAll); win.add(scrollView);
Please help ,,,, i know its easy for u all to do it :'D
4 Answers
You have to do sth like this:
var client = Ti.Network.createHTTPClient(); client.onload = function(){ // this will be called when the data is loaded // do something clever with your data // i.e. var json = JSON.parse(client.responseText); Ti.App.fireEvent('app:gotSth', {myJson: json}); }; //if any error client.onerror = function(){ // something went wrong // deal with it }; client.setTimeout(5000); client.open("POST", YOUR_URL_AS_STRING); // hop! client.send();
ok so close... the open and send need to go outside the onload function. onload will be called when the data has been returned...
Also add an onerror function to capture any errors.
have a look at the docs on the appcelerator web site, there are a couple of them which are good.
http://wiki.appcelerator.org/display/guides/Working+with+Remote+Data+Sources
hope this helps.
You can also use call back function
Thanx guys for your support,,,
the problem is that am not Familiar with the JS language :'(
i replaced the below code
var json; var response; var GET_DATA = Titanium.Network.createHTTPClient({ onload: function(){ alert("hi"), GET_DATA.open("POST","http://172.16.1.215/sites/iOS1/get_main.php"), GET_DATA.send(), json = this.responseText; response = JSON.parse(json); if (response.info) { alert(response.info); } else { alert(response.message); } } });
with as per what Mr. Greg Berger made in the previews reply and i add alert("hi"); so i can check if the code will run from the beginning of the app but nothing happend :'(
var client = Ti.Network.createHTTPClient(); client.onload = function(){ alert("hi"); // this will be called when the data is loaded // do something clever with your data // i.e. var json = JSON.parse(client.responseText); Ti.App.fireEvent('app:gotSth', {myJson: json}); var response = JSON.parse(json); }; //if any error client.onerror = function(){ // something went wrong // deal with it }; client.setTimeout(5000); client.open("POST", "http://172.16.1.215/sites/iOS1/get_main.php"); // hop! client.send();
- Note ** I don't want to send any data to the PHP page. ** what i will get back is response.info ** i want to replace the Label1 with the response.info
can any one create this code for me,,, please
Your Answer
Think you can help? Login to answer this question!