Simple Async HTTP Module for Appcelerator

Hello techies,

I have been using Appcelerator recently. It is a pretty cool tool, it allows you to create cross platform mobile applications for iOS and Android, writing JavaScript.

Appcelerator’s Titanium Framework uses the CommonJS API so you can reference javascript files as “modules”, it is very reminiscent of node.js which also implements CommonJS.

Below is a simple async module I wrote, inspired by codeboxed’s gist, to make requests to a web server. Stick it in a file named SimpleHttpAsync.js

  //////////////////////////////////////////////
 //                Simple Http Async         //
//////////////////////////////////////////////

exports.call = function (options) {
    //create the titanium HTTPClient
    var httpClient = Titanium.Network.createHTTPClient();

    //set the httpclient's properties with the provided options
    httpClient.setTimeout(options.timeout);
    httpClient.onerror = options.error;
       
    //if and when response comes back
    //the success function is called
    httpClient.onload = function(){
        options.success({
            data: httpClient.responseData, 
            text: httpClient.responseText
        });
    };
        
    //open the connection
    httpClient.open(options.type, options.url, true);

    //send the request
    httpClient.send();
};

The following is example code of importing and using the module

//import the module
var simpleHttpAsync = require('simpleHttpAsync');

//call the function
//handle errors and successful request
simpleHttpAsync.call({
    type : "POST",
    url : "http://www.someurl.com?somekey=somevalue",
    error : function (error) {
        //do something to handle error
    },
    success : function (response) {
        //do something with the response data from the server
    },
    timeout : 10000
});

It’s nothing special, but the documentation for Appcelerator is pretty sparse and I thought it might be useful for those new to the Appcelerator Titanium Framework.

As always enjoy and let me know if you have any comments/suggestions/questions. Thanks!

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Sean Biefeld

Hello all, my name is Sean Biefeld. I graduated from Baylor University with a BBA in Management Information Systems. I am currently working for McLane Advanced Technologies . The opinions expressed by me, however accurate they may be, are not necessarily those of my employer. I have been a developing software professionally since 2004. My primary development focus is on HTML, CSS, JavaScript and C#. I dabble in a little PHP and Ruby. This blog is a forum for me to post my experiences, ideas, rants, and thoughts as I traverse the mountain of life.
This entry was posted in Appcelerator, Javascript and tagged , , , , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://domenicdenicola.com/ Domenic Denicola

    Hi Sean,

    I’ve been investigating Titanium a bit myself, so this post is definitely interesting. I’m a bit confused as to what this offers over using `XMLHttpRequest` or `jQuery.ajax` though?

    Also, I think the name of the module is pretty misleading: usually when one thinks of “async” what comes to mind is stuff like this: https://github.com/joyent/node/wiki/modules#wiki-async-flow

    • Anonymous

      It is essentially a wrapper around Appcelerator’s mobile httpclient, i dont think you can use straight up XMLHttpRequest or jQuery.ajax for native portions of the mobile API, maybe in the webviews.
      Ya async is a bit vague, something like asyncHttp might be better.

  • Palakorn

    This is very good snippet!

  • Ludico8

    hi, i have a problem when data retrieves is a XML document :S in
    the success : function (response) { section i cant see the response data when i apply JSON.parse, if i dont apply this, the response data come in text format :S

  • Michael Stelly

    perfect! i was able to create an http module for my Alloy project. thx.

  • jan

    thank you as a newbie it helped enormously :)