Created
March 26, 2011 16:14
-
-
Save codeboxed/888409 to your computer and use it in GitHub Desktop.
Send requests to a server using Titanium API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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