Serialization Classes


Mobile Hessian

The Hessian protocol is so small and efficient that it can be used for communication between mobile devices and a service. The process for using Hessian in a mobile device isn't as straightforward as the online documentation would suggest. This section leads you step by step through the process of creating a J2ME client to access both the BasicService and the Authentication Service previously coded as a traditional application. The following steps include the full process:

  1. The first and most important step is installing J2ME from Sun on your development machine. For these examples, we used J2ME Wireless Toolkit 2.0, available at http://java.sun.com/products/j2mewtoolkit/ download.html. We used J2SE version 1.4. Download the toolkit, execute the installer, and follow all the steps to install J2ME.

  2. The next step is hinted at on the Caucho site under Hessian. To use Hessian for mobile devices, you must remove many of the objects in the Hessian-3.0.2.jar to produce a JAR file that contains only those classes needed for J2ME. Here are the steps to create a micro version of the JAR:

    • Using a command prompt, move to the directory where Hessian-3.0.2.jar currently is located.

    • Unjar the file with the command jar -xf Hessian-3.0.2.jar to produce a directory structure of the classes in the JAR file. The only directory in the local directory from the JAR extraction will be /com.

    • Move into the /com directory and down the hierarchy until you get to a level that has the directories hessian and services.

    • Delete the services directory.

    • Move into the hessian directory.

    • Delete the directories client, server, and mux.

    • Move into the io directory.

    • Remove all class files except HessianProtocolException.class, HessianRemote.class, and HessianServiceException.class.

    • Go back to the root directory where you did the previous JAR command.

    • Create a new JAR file with the command jar -cf microhessian-3.0.2.jar com.

  3. Various tools were installed with the J2ME toolkit. One is KToolbar, which is a simple application to build and execute an emulator which will run a J2ME application. In a Windows installation, you can launch KToolbar by clicking Start/Programs/J2ME Wireless Toolkit 2.0/KToolbar. The application is shown in Figure 9.3.

    click to expand
    Figure 9.3: KToolbar dialog.

  4. The J2ME product includes instructions for using KToolbar, so the following instructions are simple:

    • Create a new project by clicking New Project. Enter a name for the project (it becomes a directory under <installation drive>/WTK104/apps directory); use hessian. Enter a name for the class to be used to build the mobile application; use Hello. Click Create Project, and the application will build the appropriate directories for the project.

    • The project directories are in the directory <installation drive>/WTK104/apps/hessian. Two of the directories are immediately important to use. The system expects to find any non-Java-standard class libraries in /lib; this is where you need to put the microhessian-3.0.2.jar file created in step 2.

    • You need to place a Java source file in the second directory, /src. In this directory, create a file called Hello.java and enter the code from Listing 9.6.

      Listing 9.6: MicroHessian BasicAPI code.

      start example
       import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.io.*; import com.caucho.hessian.io.*; import com.caucho.hessian.micro.*; import java.io.*; public class Hello extends MIDlet {   private Display display;   public void startApp() {     display = Display.getDisplay(this);     try {       String url = "http://www.caucho.com/hessian/test/basic";       HttpConnection c = (HttpConnection) Connector.open(url);       c.setRequestMethod(HttpConnection.POST);       OutputStream os = c.openOutputStream();       MicroHessianOutput out = new MicroHessianOutput(os);       out.startCall("hello");       out.writeNull();       out.completeCall();       InputStream is = c.openInputStream();       MicroHessianInput in = new MicroHessianInput(is);       in.startReply();       String value = in.readString();       in.completeReply();       TextBox t = new TextBox("Http Test", value, value.length(), 0);       display.setCurrent(t);     } catch (Exception e) {}   }   public void pauseApp() {   }   public void destroyApp (boolean unconditional) {   } } 
      end example

    • Click the Build button in KToolbar. After a successful build, click Run. A cell phone emulator will appear, as shown in Figure 9.4. Click the middle button among the arrow keys on the emulator to launch the hessian application.

      click to expand
      Figure 9.4: Cellphone emulation.

    • You should see the familiar Hello World, just as you saw in the BasicAPI example earlier in the chapter.

What's in the Code

Let's discuss the Mobile Hessian code in Listing 9.6, because it is little different from the previous Hessian client code. All the code for using the BasicAPI Web service is found in the startApp() method, as required by the J2ME specification.

The first statement in the startApp() method obtains an object pointer to the cell phone display and stores it in the display variable. Next, the code attempts to create an HTTP connection to the Web service using the HttpConnection class.

Once the connection is opened, an OutputStream object is attached to the connection as well as a MicroHessianOutput object. The object's startCall() method is used to pass the method you want to access—in this case, the hello() method.

After the call is made, the reply from the Web service is obtained using the following statements:

        in.startReply();        String value = in.readString();        in.completeReply(); 

Finally, the reply is written to a textbox and displayed on the cell phone. Although this is a simple example, it shows how even small devices can be used to access Web services and transfer information back and forth.




Mastering Resin
Mastering Resin
ISBN: 0471431036
EAN: 2147483647
Year: 2002
Pages: 180

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net