Need to load JSON data from the beginning of the App ,,, please help

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

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.

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

— answered 2 years ago by Mubarak Al-Mutawa
answer permalink
2 Comments
  • Well... I could also code your entire app. But that would be more expensive ;-)

    No, seriously: First: be sure that your php script is returning data. I tried to reach the URL through my browser and got a timeout error. Try to log the error in the client.onerror function.

    second: change the "POST" parameter of client.open() method to "GET"

    third: in order to get the label changed. i would either change the label1.text inside the client.onload or fire a custom event with the data you just retrieved (as i did in my example), somewhere else in you code, add an eventlistener in whitch you would change the label.

    — commented 2 years ago by Greg Berger

  • Hi i am working with Soap call: I want to do a soap call to fetch data from server. In my case i want to fetch data from sugarCRM framework. But unfortunately i m not able to pass multidimensional json format to the server via sudonclient object. I have tried the same thing in PHP and it returns me exaclty what i want. I want to pass a selected field array to the server for field selection in mysql result. It working fine in php but not in titanium using json format.

    PHP code $product_list_params = array( 'session' => $session, 'module_name' => 'Accounts', 'query' => '', 'order_by'=>'', 'offset' => 0, 'select_fields' => array('id','name'), 'max_results' => 1, 'deleted' => 0 ); //parameters array

    $product_list_array = $client->call('get_entry_list',$product_list_params); //make call

    Titanium Code

    var data='{"session":"'+session_id+'","module_name":"'+m_name+'","query":"","order_by":"name asc","offset":"2","max_results":"10","deleted":"0","select_fields":["id","name"]}';

    var list=JSON.parse(data); //parse data

    suds.invoke('get_entry_list',list,function(xmlDoc){

    //But here i dont got any response data in

    var retNode = xmlDoc.getElementsByTagName('return').item(0);

    });

    — commented 1 year ago by rajveer singh

Your Answer

Think you can help? Login to answer this question!