Send requests to a server using TideSDK

by THavamani 2013-04-29 17:18:08

We have created a small snippet for sending synchronous HTTP requests to a server using the Ti.Network.HTTPClient object for Desktop.


var Call = function(options) {
//Private
var serverURL = options.url,
method = options.method,
timeout = options.timeout;

/**
* Private method used to send the request to the server
* @param {object} params
* @returns string|boolean|null
*/
var callAction = function(params) {
var httpClient = Ti.Network.createHTTPClient(),
result = null;

httpClient.setTimeout(timeout);

httpClient.onerror = function(e) {
Ti.API.info('HTTP error: ' + e);
};

if (httpClient.open(method, serverURL, false)) {
httpClient.receive(function(response) {
result = response.toString();
}, params);
} else {
Ti.API.info('cannot open connection');
result = false;
}

return result;
};

//Public
return {
call: function(params) {
return callAction(params);
}
};
};

// HOW TO USE
var initOptions = {
url: 'http://www.example.com',
method: 'POST',
timeout: 2000
};

var callObject = new Call(initOptions);

var callParams = {
var1: 'var1',
var2: 'var2'
};

var response = callObject.call(callParams);
743
like
0
dislike
0
mail
flag

You must LOGIN to add comments