Browse DevX
Sign up for e-mail newsletters from DevX


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

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.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Step 1: Invoking a Content Handler
Invoking a content handler is a simple task. The example below is based on the examples included on the CHAPI specification document. In the example, the application requests activation of a content manipulator for the vcard (electronic business card) file type (see http://www.imc.org/pdi/) using an Invocation object instance.

// Invoke based on a URL try { String url = "http://host/jones.vcard"; Invocation invoc = new Invocation(url); invoc.setAction(ContentHandler.ACTION_OPEN); invoc.setArgs(new String[] {"debug"}); if (Registry.invoke(invoc, this)) { // App must exit before invoked application can run notifyDestroyed(); // inform the application manager } else { // Application does not need to exit Invocation ret = Registry.getResponse(this, 1000); // handle the returned invocation result here! Alert alert = new Alert("", """Content handling status: " + ret.getStatus() + " for content " + ret.getUrl(), null, null); display.setCurrent(alert); } } catch (IOException ex) { Alert alert = new Alert("", """Could not go there", null, null); display.setCurrent(alert); }

In the preceding code, the invoker specifies the opening action (ContentHandler.ACTION_OPEN) and provides some additional arguments that the content handler can use to perform business-specific operations.

Applications invoke handlers using the Registry.invoke() method. This method returns a Boolean value, which indicates whether the invoker must be closed before the content handling takes place.

The example also includes handling an expected response. You can retrieve the Invocation object containing the result of the processing made by the content handler by calling the Registry.getResponse() method. Then you can access all the Invocation attributes (action, args, url, status, etc.) freely, which makes handling the response a rather simple procedure.

It is important to note that the only mandatory connection model implementation on MIDP 2.0 compliant devices is the HttpConnection; however, it's possible to use other protocols on devices that have specific stack implementations, because CHAPI relies on the Generic Connection Framework model (the standard mechanism for creating connections on J2ME). For example, a device that implements JSR 75 (and thus supports a file system) should be able to request a content handler using the file protocol (e.g. "file:///MMC:/mypicture.gif") when instantiating the Invocation object.

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