Skip to content

Instantly share code, notes, and snippets.

@codeboxed
Created March 26, 2011 16:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codeboxed/888409 to your computer and use it in GitHub Desktop.
Save codeboxed/888409 to your computer and use it in GitHub Desktop.
Send requests to a server using Titanium API
/**
* Filename: Call.js
* Author: Codeboxed
* URL: http://www.codeboxed.com
* Date: March 26, 2011
* Platform: Titanium
*/
/**
* Call
* @param {object} options
*/
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 = Titanium.Network.createHTTPClient(),
result = null;
httpClient.setTimeout(timeout);
httpClient.onerror = function(e) {
Titanium.API.info('HTTP error: ' + e);
};
if (httpClient.open(method, serverURL, false)) {
httpClient.receive(function(response) {
result = response.toString();
}, params);
} else {
Titanium.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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment