Grizzly, Async HTTP Client, Atmosphere, and Android

I’ve realized that this is only my second entry for 2012 – it certainly doesn’t seem like it’s been that long since January.  I’ll try to be better about posting more often.  That having been said, for some time I’ve been wondering if Grizzly could work on Android. More specifically, if Async HTTP Client’s Grizzly provider could be used as a websocket client on that platform. I’ve recently had some time to find out.

DISCLAIMER: I am *not* an Android developer, so don’t be surprised if the code I show is not inline with the usual best practices when developing on that platform. I just wanted a quick and dirty proof of concept.

So for the test, I used the following software packages:

  • Async HTTP Client (referred to as AHC for the remainder of this entry) 1.7.6 (This depends on Grizzly 2.2.16)
  • Atmosphere 1.0.0 (specifically, the rest-chat sample)
  • GlassFish 3.1.2.2

I’m not going to go into too many details on the GlassFish/Atmosphere side of things outside of make sure comet and websocket support is enabled for http-listener-1, start the server, and deploy the atmosphere-rest-chat web application to GlassFish.

Now for the Android side of things, which, as I was not familiar with programming on Android, turned out to be entertaining to do.  I used Intellij 12’s Android support to develop the UI and the Activity class which would handle all of the interactions for a simple websocket client.  This client performs four basic functions: connect, disconnect, send messages, display messages.

The implementation of these functions was performed by two different AsyncTask implementations (see the Android developer SDK documentation for more details).  Connect/Disconnect and message display were merged into one task since the AHC WebSocketListener implementation was added at the time of connection.

The other task is the SendMessage task which will be invoked when the ‘Send’ button is pressed:

The last bit of info you’ll need to make this work is that a special configuration option is needed in the AndroidManifest.xml in order for network IO to be allowed:

With the code above (well, most of it anyway) in place we should be able to have a chat conversation between Android and browser clients:

Here is the browser’s initial state after entering the user’s chat name:

Here is the Android client’s initial state after connecting and sending a message:

And now the browser’s view:

And then what Android sees when the browser responds:

There!  Not too difficult, right?

I found it satisfying to know that AHC+Grizzly may be used as a client solution for custom websocket applications on Android.  Hopefully with this basic information being made available, it will prompt more users to explore this option.

Lastly, if there is any interest in having the complete code to play around with, follow up to the post and if there’s enough interest, I’ll push the code to github.

 

This entry was posted in Java and tagged , , , , , . Bookmark the permalink.

5 Responses to Grizzly, Async HTTP Client, Atmosphere, and Android

  1. graham wood says:

    Hi,

    Greate article. Please release the source code.

    Thank you!

  2. graham wood says:

    Hi,

    Could you please push the src code to github ?

  3. Phil T. says:

    Hi Ryan!

    Nice job!

    Can you please state which Android SDK version you’ve used?

     

    Thanks in advance!

    Phil