public
Last active

Send requests to a server using Titanium API

  • Download Gist
call.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/**
* 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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.