Sunday, July 13, 2014

titanium - getting data from Parse. like SELECT

var url = "https://api.parse.com/1/classes/Hello"; // SELECT *

 var client = Ti.Network.createHTTPClient({
     // function called when the response data is available
     onload : function(e) {
         Ti.API.info("Received text: \n" + this.responseText);
         textarea.value = this.responseText;
         var response = JSON.parse(this.responseText);
         Ti.API.info("Received object: \n" + response);
         //Ti.API.info("Received xml: \n" + this.responseXML);
         //alert('success');
         //popup.close();
         activityIndicator.hide();
         //process(this.responseXML);
     },
     // function called when an error occurs, including a timeout
     onerror : function(e) {
         Ti.API.debug(e.error);
         textarea.value = e.error;
         //alert('error');
         //popup.close();
         activityIndicator.hide();
       
     },
     timeout : 5000  // in milliseconds
 });
 // Prepare the connection.
 //client.open("POST", url); // sending data to Parse
 client.open("GET", url); // getting result from Parse
 // copied from Parse online settings > Application keys
 client.setRequestHeader("X-Parse-Application-Id", "/** copied from parse.com **/");
 // copied from Parse online settings > Application keys
  client.setRequestHeader("X-Parse-REST-API-Key", "/** copied from parse.com **/");
   //client.setRequestHeader("Content-Type", "application/json");
 client.send();

JSON string returned.
eg.:
{"results":[
{"Description":"exciting book","price":0.99,"title":"my book","createdAt":"2014-07-08T01:48:15.369Z","updatedAt":"2014-07-08T01:48:35.968Z","objectId":"xEVblKpjTC"},
{"Description":"jack throw beans and stalk grew and he climbed and met giant","price":1.5,"title":"jack and the beanstalk","createdAt":"2014-07-13T14:55:02.571Z","updatedAt":"2014-07-13T14:55:30.630Z","objectId":"SKv9CbqKzi"},
{"title":"lord of the ring","price":15.99,"description":"frodo saves the middle-earth","createdAt":"2014-07-13T15:14:22.467Z","updatedAt":"2014-07-13T15:14:22.467Z","objectId":"JKWFuRnf0T"}]}

if need specific object, use the Id in url
eg.: https://api.parse.com/1/classes/Hello/xEVblKpjTC
response: {"Description":"exciting book","createdAt":"2014-07-08T01:48:15.369Z","objectId":"xEVblKpjTC","price":0.99,"title":"my book","updatedAt":"2014-07-08T01:48:35.968Z"}

No comments: