C H A P T E R  7

Coding the Currency Converter Wireless Application

This section of the tutorial shows you how to modify the Currency Converter application you created in Chapter 4 so that it can operate as client/server application. The chapter takes you through the tasks necessary to build a Web Module, modify the Currency Converter application, create the connecting code using the J2ME Wireless Connection Wizard.

The sections in this chapter are:


Initial Setup

For this tutorial, you need to mount the filesystem where your Currency Converter application is stored. If it is not mounted, follow these instructions.

To mount the Currency Converter filesystem:

1. Choose Fileright arrowMount Filesystem.

This opens the Mounting wizard.

2. Choose Local directory and click Next.

3. In the Select Items to Mount page, navigate to the filesystem where you created Currency Converter.

This tutorial will use a filesystem in the default Java Studio Mobility user directory for a user named JavaCoder on the Windows platform:
c:\Documents and Settings\JavaCoder\jstudio_6me_user\examples.

4. Click Finish.

The examples filesystem is mounted.

Screenshot of the explorer window showing mounted filesystem sampledir and the filesystems for the currency Converter Wireless Connection Example wizard.  


Creating a Web Module

Next, create the Web Module that stores the conversion rates for the Currency Converter.

1. Right-click the examples filesystem and choose Newright arrowAll Templates.

This opens the New wizard.

2. In the Choose template page, select the Folder template. Click Next.

3. Name the new folder myserver. Click Finish.

4. Right-click on the myserver folder and choose Newright arrowAll Templates.

5. Expand the JSPs & Servlets node and select Web Module. Click Next.

6. In the Choose Target page, make sure the directory path points to the myserver folder. Click Finish.

The Web Module is created and the filesystems myserver and myserver: /WEB-INF/classes are shown in the Explorer window.

Screenshot of the explorer window showing additional filesystems /server and /server : WEB-INF/classes. 

The Web Module requires additional classes to perform the functions it needs to perform.

7. Right-click the myserver: /WEB-INF/classes filesystem and choose Newright arrowAll Templates.

This opens the New wizard.

8. In the Choose template page, select the Folder template. Click Next.

9. Name the new folder myserver. Click Finish.

10. Right-click on the folder you just created and choose Newright arrowAll Templates.

11. Expand the Java Classes node and select the Java Class template. Click Next.

12. Name the class ExchangeRatesService. Click Finish.

The class is shown in the Explorer window as a subnode of server: WEB-INF/classes.

13. Double-click on ExchangeRatesService to display the code for the class in the Source Editor window. Replace the code so the final class file looks like the following:

package myserver;
public class ExchangeRatesService {
    public String[] currencies = new String[] { "US $", "GB \u00a3", "Yen \u00a5", "Euro \u20ac", "AU $", "CA $", "CZK" };
    public long[][] rates =  {{   1000000,  631114, 117580000,    911079,   1530220,  1406700, 29270000 },
                              {   1584500, 1000000, 186305000,   1443600,   2424630,  2228920, 47192000 },
                              {      8504,    5368,   1000000,      7749,     13014,    11964,   246858 },
                              {   1097600,  692711, 129056000,   1000000,   1679570,  1543990, 31251600 },
                              {    653501,  412434,  76838600,    595391,   1000000,  1087810, 17172700 },
                              {    710884,  448694,  83585700,    647671,    919280,  1000000, 19090800 },
                              {     34165,   21190,   4050910,     31998,     58232,    52381,  1000000 }};
    /** Creates a new instance of ExchangeRatesService */
    public ExchangeRatesService() {
    }
    public int getCurrenciesNum() {
        return currencies.length;
    }
    public String getCurrencyName(int index) {
        return currencies[index];
    }
    public long getRate(int from, int to) {
        return rates[from][to];
    }
}

This file now contains the exchange rates.

The next step is to create the MIDP class that will extend the Web services to the Currency Converter MIDlet. This is where you use the J2ME Wireless Connection wizard.


Extending Web Services to a MIDlet.

1. Right-click the myconverter package and choose Newright arrowJ2ME Wireless Connection Mapping.

This opens the J2ME Wireless Connection Mapping wizard.

2. In the New Object Name page, accept the default value. Click Next.

3. In the Client Target Location page, enter RatesUpdater for the generated J2ME class, and choose the examples/myconverter package. Click Next.

4. In the Servlet Target Location page, enter RatesUpdaterServlet for the generated servlet class and choose the myserver: /WEB-INF/classes filesystem. Click Next.

5. In the Code Generation Options, check the Generate Stub Methods, Enable Multiple Calls per Connection, and Supports CLDC 1.1 Data Types checkboxes. Click Next.

6. In the Select Exported Services page, expand ExchangeRatesServices so that you see the three services:

7. Select all three services and click the Add button.

The services are shown in the Selected Services pane.

8. Click Next.

The Summary page lists all the changes you have designated. Notice that it will also generate a mapping file named EndToEndMapping.xml. Click Finish.

A dialog appears that lists the files created. Another dialog appears asking you to confirm changes in the deployment descriptor.

9. Click Process All to confirm the changes, then click OK to dismiss the second dialog.

Screenshot of Information dialog. The button is OK. 

10. Modify Converter.java:

a. Open Converter.java and add the following two imports:

import java.io.IOException;
import java.util.Enumeration;

b. Add Runnable to the list of interfaces that the Converter class implements:

public class Converter extends Form implements CommandListener, ItemStateListener, Runnable {

c. Add the method new command() into constructor after the super call:

 addCommand(new Command("Update Data", Command.SCREEN, 1));

d. Modify the commandAction() method to handle new command() by inserting the following code:

} else if (command.getCommandType() == Command.SCREEN) {
            Display.getDisplay(midlet).setCurrent(new Form("Please wait..."));
           new Thread(this).start();
        }
 

e. Add a new method implementing Runnable:

public void run() {
        try {
            RatesUpdater upd = new RatesUpdater();
            int size = upd.myserver_ExchangeRatesService_getCurrenciesNum();
            midlet.currencies = new String[size];
            midlet.selected = new boolean[size];
            midlet.rates = new long[size][];
            for (int i=0; i<size; i++) {
                upd.myserver_ExchangeRatesService_getCurrencyNameGrouped(i);
                for (int j=0; j<size; j++) {
                    upd.myserver_ExchangeRatesService_getRateGrouped(i, j);
                }
            }
            Enumeration results = upd.getGroupedResults();
            for (int i=0; i<size; i++) {
                midlet.currencies[i] = ((String)results.nextElement());
                midlet.selected[i] = true;
                midlet.rates[i] = new long[size];
                for (int j=0; j<size; j++) {
                    midlet.rates[i][j] = ((Long)results.nextElement()).longValue();
                }
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        midlet.notifySettingsChanged();
    }

This completes the coding for the Currency Converter application.


Executing the MIDlet

Executing the MIDlet is the same procedure as before.

1. Right-click on the converter MIDlet suite and choose Execute.

The Execute function compiles the MIDlet application, if necessary, before it executes the application.

As with the first Currency Converter application, the MIDlet suite runs on the default DefaultColorPhone skin within the J2ME Wireless Toolkit 2.1 installation.

Screenshot of the DefaultColorPhone device emulator, with Currency Converter application displayed in the device.A callout points to the position of the Launch button. 

Now you're ready to test the application in the device emulator.

2. Select the currency you want to convert by clicking the up and down arrow keys on the Select button.

You can select Dollars, Euros, or Yen.

3. Enter the currency amount to convert by clicking the emulator's numeric keys.

The application makes the conversion calculations and displays the results.

4. Click the button underneath the word "Exit" to exit the application.

Screenshot showing the Converter application running on the default device emulator. 


Summary

In this chapter, you used the J2ME Wireless Connector wizard to extend the services Currency Converter MIDlet into a mobile client application that accesses services on a web server.