I'm building a Service Android with appcelerator studio.
In this service a call a web service that return a json.
So this is the code to call ws:
funtcion start(){
var obj = getDocument("CFDECTEST02",NULL);
Titanium.API.info("DOCUMENT "+ obj);
}
function getDocument(fiscalCode, date){
//se il servizio può partire, procedo con il chiamare il ws
// create request
var obj;
var xhr = Titanium.Network.createHTTPClient();
//set timeout
xhr.setTimeout(10000);
//Here you set the webservice address and method
xhr.open('POST', "http://Document/");
//set enconding
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
var myObject = {
cf :fiscalCode,
date_last_synchronization :date
};
//send request with parameters
xhr.send(JSON.stringify(myObject));
// function to deal with errors
xhr.onerror = function() {
Ti.API.info("SERVIZIO IN ERRORE");
Ti.API.info(this.responseText);
disattivaSemaforo();
};
// function to deal with response
xhr.onload = function() {
var obj = JSON.parse(this.responseText);
Ti.API.info(this.responseText);
return obj;
};
}
Now I want that if the WS return the correct response I return the response to the method start.
Then if when I try to call web service, it is all ok, the method
xhr.onload = function() {
is execute, and I can do return obj to start() method.
It is possibile to do this?