Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Map J2ME Applications to Content Types with JSR-211 (CHAPI) : Page 3

If you've been frustrated when trying to write J2ME apps that communicate with other applications, help is at hand. This brand new J2ME API improves the way mobile devices handle content.

Step 2: Handling a Request
Handling a request is also a simple task. The example below shows an application which handles a request and then displays the result to users until they press a key to close the content handler.

public class VCardViewer extends MIDlet implements ContentListener, CommandListener { Invocation invoc; ContentHandlerServer handler; public VCardViewer() { // Register the listener to be notified of new requests handler = Registry.forClass(this.getClass().getName()); handler.setListener(this); } // Process a queued Invocation. public void invocationNotify() { // Dequeue the next invocation invoc = handler.getRequest(0L); if (invoc!= null) { // Display the content of the VCard displayVCard(invoc); } } public void commandAction(Command c, Displayable s) { if (c == okCommand) { if (invoc!= null) { if (handler.finish(invoc, Invocation.OK)) { // inform the application manager notifyDestroyed(); } invoc = null; } } } void displayVCard(Invocation invoc) throws Exception { HttpConnection conn = (HttpConnection)invoc.open(false); VCard vcard = parseVCard(conn.getInputStream()); ... // Display the content based on the action String action = invoc.getAction(); if (ContentHandler.ACTION_OPEN.equals(action)) { showView(vcard); } if (action.getArgs()!= null && action.getArgs()[0].equals("debug")) { System.out.println("Application on debug mode!"); } } }

During the example initialization, the call to the Registry.forClass() method instantiates a ContentHandler object associated with the MIDlet (the attributes of this instance, such as supported types and actions, are determined when the MIDlet is registered as a content handler. I'll discuss the procedure later on this article). Using the instance returned from the Registry.forClass method, you can access the pending requests and process them as desired. The ContentHandler class (and its direct subclass, ContentHandlerServer notifies the MIDlet about pending requests via the invocationNotify() method exposed by the ContentListener class (following the request-response model commonly adopted on other APIs, such as the Wireless Messaging API), and can retrieve the invocation data by calling the getRequest() method.

After accessing the Invocation object, you can retrieve the content as a stream of data using the Generic Connection Framework. The Invocation object also grants access to all the attributes which were configured during the instantiation of the Invocation: action, arguments, status, etc.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date