cometd chat screenie
I have ported the cometd java client from Jetty to run on the google android phone.  Cometd implements the bayeux protocol for push messaging over HTTP and is normally used by Ajax clients running in the browser. By porting the java client to android, this allows native applications on the phone to have bidirectional messaging to a server over HTTP.
To demonstrate this, I’ve implemented a simple chat room that interacts with the Ajax Comet chat room demos. This would be a great basis for other android applications that need to update in realtime.
The following core dependencies were used:

  • jetty-client-6.1.12.rc2 (asynchronous http client)
  • bayeux-client-6.1.12.rc2 (bayeux protocol via http)
  • android-1.0_r1

I had been wrestling with android for a couple of days trying to get familiar with their apis. The more you dive into it, you would notice a pattern that you might be used to, which is developing webapps using the MVC pattern .

View = xml resources (like html .. static rendering)
Activity = Controller (logic)

To get started, you can use the ADT (Android Developer Tools) eclipse plugin. Although it could speed up your productivity, it would can up your cpu and memory as well. It takes a few seconds to recompile(annoying) even with just the slightest change in code.
The
import part of the plugin is the auto-update of the R.class of your
application (after adding/editing/removing your views and other xml
resources).
Implementing the chat client using cometd-jetty client was a breeze because all the work (bayex protocol) was already done by org.mortbay.cometd.client.BayeuxClient.
The one problem I’ve encountered was updating android’s UI from another thread (e.g message received event). If I had read all the android FAQs, I wouldn’t have been stuck on it. Oh well …so the solution was simply creating an android.os.Handler inside your Activity to be able to receive asynchronous events (dispatched from another thread) and to be able to render/update your UI.
public class MyActivity extends Activity
{
public static final int SOME_EVENT = 10;


Handler _handler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
switch(msg.what)
{
case SOME_EVENT:
// update your ui here
break;
}
}
}
Runnable _someEventHandler = new Runnable()
{
public void run()
{
_handler.sendEmptyMessage(SOME_EVENT);
}
}
…..
}

The source and apk is available here.
To chat live (after installation), change your connection settings:
host: cometdchat.morphexchange.com
port: 80
Enjoy!


20 Comments

Meirbek (Mike) · 31/10/2008 at 23:11

I want to implement an app for android for one of my Software Development classes, where i need a chatting feature. Hope this will help…
thanks, dyu

Ben Krembs · 12/11/2008 at 04:09

Hmm.. great idea, but I think your instructions are missing something.

1. I tried running the MVN build, but got and error “Patch command failed (exit value != 0) for cookie.patch” (full stacktrace is here: http://pastebin.com/m5868c281 )

2. I instead tried setting the project up in Eclipse (using the Android plugin) and it compiles/runs, but get an error about a missing org/mortbay/jetty/mime resource bundle (full stacktrace here: http://pastebin.com/m1568ed4 )

Help? I’d love to see this in action.

Laura · 16/11/2008 at 17:52

Hey.. sounds nice, but this doesn’t work when building from source.

I ran the MVN exactly as is and got the following exception when I clicked “Join Chat Room”

W/dalvikvm( 237): Link of class ‘Lorg/mortbay/cometd/client/BayeuxClient;’ failed
I/dalvikvm( 237): Failed resolving Lorg/mortbay/cometd/client/BayeuxClient; interface 300 ‘Lorg/cometd/Client;’
W/dalvikvm( 237): Link of class ‘Lorg/mortbay/cometd/client/BayeuxClient;’ failed
W/dalvikvm( 237): VFY: unable to resolve virtual method 1717: Lorg/mortbay/cometd/client/BayeuxClient;.publish (Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)V
W/dalvikvm( 237): VFY: rejecting opcode 0x6e at 0x0025
W/dalvikvm( 237): VFY: rejected Lorg/mortbay/demos/android/cometdchat/util/ChatRoomClient;.chat (Ljava/lang/String;)Z
W/dalvikvm( 237): Verifier rejected class Lorg/mortbay/demos/android/cometdchat/util/ChatRoomClient;

Your provided .apk worked, however.

dyu · 03/12/2008 at 06:36

Ben,
Unlike unix/linux, you must have the gnu patch installed when building on windows. These patches are fixes for the runtime errors throw by the davlik jvm. The second error you got was basically the result of the patch not being applied.
Cheers,
dyu

dyu · 03/12/2008 at 06:44

Laura,
That’s weird. I’m guessing that when you built it, there were compilation issues(since the apk worked).
I built it on jdk1.5, windows xp and android-1.0_r1 if that helps.
Cheers

chipsyKing · 08/01/2009 at 15:34

hmm, the client is running, but every time when i click on join chat room i get an exception popup at the emulator.
Can anybody help me?
Cheers

Jerry · 26/04/2009 at 11:21

Hi,
I try to run your sample, but I can not compile , it looks like missing some Jars in the eclipse project .
I’d like to ask you the following question, could you tell me what kind of Jars needs . can you advice me , thanks
Jerry
The following core dependencies were used:
jetty-client-6.1.12.rc2 (asynchronous http client)
bayeux-client-6.1.12.rc2 (bayeux protocol via http)
android-1.0_r1

dyu · 27/04/2009 at 03:16

@Jerry
I used maven2 to resolve the dependencies … it is listed in the pom.xml.
(the jetty jars and cometd-client jars)

Kevin · 30/04/2009 at 06:27

How about your demo server? Is it write by yourself? I use the jetty demo server, I can use android client connect to the server.

Jerry · 30/04/2009 at 06:42

Thanks for you repl, but I get the following error in the android log ,and display can not join the chat room, Can you advice me ?
04-30 14:18:35.049: WARN/Jetty(244): org.mortbay.cometd.client.BayeuxClient@4337c730 {“content”:”nnnError 404 NOT_FOUNDnn

HTTP ERROR 404

n

Problem accessing /cometd/handshake. Reason:n

    NOT_FOUND


Powered by Jetty://
n
n

fatemeh · 28/05/2009 at 04:33

Hi.That’s great! But … where is the main class?

dyu · 28/05/2009 at 07:29

@Kevin

I used the same jetty cometd demo you used. No custom code on the server side.

@Jerry

You need to fire up the jetty cometd demo

Basically grab a jetty distro in http://dist.codehaus.org/jetty/

After you extract it the bundle, start jetty with $ java -jar start.jar

@fatemeh

You mean CometdChatApplication.java?

fatemeh · 28/05/2009 at 08:41

sorry i know it’s clear for you but i don’t know how run this code on eclipse!!

dyu · 28/05/2009 at 09:16

@fatameh

You need to import this project to eclipse using the android eclipse plugin and run it using the same plugin.
E.g right click the AndroidManifest and click ‘run’

fatemeh · 29/05/2009 at 09:19

” @Jerry I used maven2 to resolve the dependencies … it is listed in the pom.xml. (the jetty jars and cometd-client jars) ”
I did everything that you said, but still the project didn’t recognize some jars like :
org.cometd.Bayeux;
org.cometd.Client;
org.cometd.ClientListener;
org.cometd.Extension;
org.cometd.Message;
org.cometd.MessageListener;
org.cometd.RemoveListener;
I think I must change some lines from pom.xml. is’nt it? if yes;…how I do?

fatemeh · 03/06/2009 at 11:33

i found that jars..but org.mortbay.jetty.client.HttpClient dont have this methods:
httpClient.getAttribute
httpClient.setAttribute
and you use it! how?

dyu · 04/06/2009 at 02:13

Are you using the same jetty version (6.1.12.rc2) in the pom.xml?

I don’t see anywhere in the code where httpClient.getAttribute is used.

fatemeh · 07/06/2009 at 11:06

That’s right! I used other version of jetty! thanks alot!
now this errors have been shown
(skipping index file ‘C:Program FilesApache Software Foundationapache-maven-2.0.10binCometdChatresdrawableThumbs.db’)
[2009-06-08 07:32:22 – CometdChat] C:Program FilesApache Software Foundationapache-maven-2.0.10binCometdChatreslayoutmain.xml:7: ERROR Error: No resource found that matches the given name (at ‘text’ with value ‘@string/hello’).
why? what is your opinion?

fatemeh · 08/06/2009 at 09:15

Hi! again I have a question!
that codes didnt recognize this library :
org.mortbay.demos.android.
cometdchat.R
thank you

fatemeh · 08/06/2009 at 09:47

Hi! again I have a question!
that codes didnt recognize this library :
org.mortbay.demos.android.
cometdchat.R
thank you

Comments are closed.