Retrieving Data from Web Server

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

Hi all..

I want to retrieve Data from Web Server..

For example U can say Database..

I have items stored in my DB... If I have 10 books in the DB... Cx picks when in the App,

I want a message to be displayed there as "Number of stocks as only 10".....

This is to check the availability of the Product in the DB...

Please any one guide me with a code......

3 Answers

Accepted Answer

Hi Suresh! you can use this code in titanium end to fetch data from XML.

-you have write CMS at php end which will update the XML.

var xhr = Titanium.Network.createHTTPClient();
 
xhr.onload = function()
{
    Ti.API.info('twitter xml ' + this.responseXML + ' text ' + this.responseText);
    var doc = this.responseXML.documentElement;
    var elements = doc.getElementsByTagName("screen_name");
    var screenName = elements.item(0);
    Ti.API.info("screenname = " + screenName.text);
 
    var screenname = Ti.UI.createLabel({
        textAlign:'center',
        height:'auto',
        width:'auto',
        top:20,
        text:screenName.text
    });
    Ti.UI.currentWindow.add(screenname);
 
    var textarea = Ti.UI.createTextArea({borderRadius:5,borderWidth:2,borderColor:'#999',backgroundColor:'#111',color:'yellow',bottom:10,left:10,right:10,height:300,font:{fontFamily:'courier',fontSize:10}});
    textarea.value = this.responseText;
    Ti.UI.currentWindow.add(textarea);
};
// open the client
xhr.open('GET','http://twitter.com/statuses/show/123.xml');
 
// send the data
xhr.send();

— answered 10 months ago by Abdus Sattar
answer permalink
4 Comments
  • in above example twitter XML is sample to test you, you can use your XML name in place of twitter XML. OK?

    — commented 10 months ago by Abdus Sattar

  • Ok..... Using this code, I can display data from XML....

    But I want to display items from the Database..

    The Database is created using PHPMyAdmin...

    How am i display the items in PHPMyAdmin??????

    — commented 10 months ago by Suresh Kumar S

  • you can write the data of MySQL server in XML using php. it is php end question, I'll share it after I bring this code from my php team. you can try by yourself. this is appcelerator blog, php end is your task now :)

    — commented 10 months ago by Abdus Sattar

  • Show 1 more comment

For getting data from web server you need to create web service on server end, you can either make get or post service which can return json or xml as per your requirement. In your mobile app you can call service using create http client object and can get json or xml in response.

— answered 10 months ago by Moiz Chhatriwala
answer permalink
1 Comment
  • I have created my database using PHPMyAdmin... I have exported that file from PHPMyAdmin....

    Is this right?????

    Or tell me how can i create a web server????

    — commented 10 months ago by Suresh Kumar S

here is php code for you Suresh!

//Suppose I am passing User Email (Known)
    $query = "SELECT * FROM login WHERE email_id='$userEmail'";
    $result = mysql_query($query) or die(mysql_error());
    $out_put = '';
    if (mysql_num_rows($result) > 0) {
        $login_selected = mysql_fetch_array($result);
        $out_put.='<loginInfo>';
        $out_put.='<userId>' . stripslashes($login_selected['id']) . '</userId>';
        $out_put.='<userName>' . stripslashes($login_selected['first_name']) . ' ' . stripslashes($login_selected['last_name']) . '</userName>';
        $out_put.='<userEmail>' . stripslashes($login_selected['email_id']) . '</userEmail>';
        $out_put.='</loginInfo>';
    } else {
        $out_put.='<loginInfo>';
        $out_put.='<userId>empty</userId>';
        $out_put.='<userName>empty</userName>';
        $out_put.='<userEmail>empty</userEmail>';
        $out_put.='</loginInfo>';
    }
    $file_handler = fopen('your_desired_file_name.xml', 'w');
    fwrite($file_handler, $out_put);
    fclose($fh);

— answered 10 months ago by Abdus Sattar
answer permalink
1 Comment
  • Please have a look at http://developer.appcelerator.com/question/140284/cannot-call-data-from-web-service-using-json

    Help me....

    — commented 10 months ago by Suresh Kumar S

Your Answer

Think you can help? Login to answer this question!