It seems like an eternity since I last tried J2ME or JavaME as its known now. It was Sept 2003, when I was working at a product development company and was building a component using J2ME. I even managed to get an article published at http://my.advisor.com/articles.nsf/aid/12697.
When I tinkered with Android a few weeks back, I got the urge right then to revisit JavaME. I wanted to try out a JavaME example again and see if anything has changed. I still believe that JavaME will eventually die out in favor of a more full featured platform (whether it is Java SDK or something else I dunno). For a primer on JavaME stacks you can check my article on advisor above. It surprising how little has changed.
In this example I will build a JavaME application using Netbeans. The application will present the user with a screen to enter a ISBN number for a book. It will then make a remote web service call to validate that the ISBN number is valid or not. When I tried a similar webservice example in 2003, web services was not yet in the optional stack. Now it is. Previously I was using ksoap. Now I do not need to. I can use the built-in libraries (if the device supports that… and that is the big headache with either JavaME or Android….device capabilities).
Create a new NetBeans MIDP project named as ISBNValidator, using CLDC-1.1 and MIDP-2.1 configuration. The IDE will create a ISBNValidatorMidlet. Netbeans will throw you into a page flow designer. I switched to the source code view. Change the code to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
package com.test; import java.rmi.RemoteException; public class ISBNValidator extends MIDlet implements CommandListener { private EnterISBNNumberForm isbnForm; public ISBNValidator() { isbnForm = new EnterISBNNumberForm("ISBN Validator", this); } protected void destroyApp(boolean arg0) { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { Display.getDisplay(this).setCurrent(isbnForm); } public void commandAction(Command cmd, Displayable disp) { if (cmd.getCommandType() == Command.EXIT) { destroyApp(false); notifyDestroyed(); } else if (cmd.getLabel().equalsIgnoreCase("Check ISBN")) { final MIDlet parent = this; new Thread() { public void run() { String result = validateISBN(isbnForm.getIsbnNumber()); String msg = "ISBN Validd => " + isbnForm.getIsbnNumber() + ", is "; ISBNValidatorResultForm resultForm = new ISBNValidatorResultForm( msg, result, (CommandListener) parent); Display.getDisplay(parent).setCurrent(resultForm); } }.start(); } else if (cmd.getLabel().equalsIgnoreCase("Main")) { Display.getDisplay(this).setCurrent(isbnForm); } } private String validateISBN(String isbn) { ISBNService_Stub stub = new ISBNService_Stub(); String result = "bad isbn"; if (isbn == null || (isbn.trim().length() != 10 && isbn.trim().length() != 13)) { return result; } try { if (isbn.trim().length() == 10 && stub.IsValidISBN10(isbn)) { result = "good isbn"; } else if (isbn.trim().length() == 13 && stub.IsValidISBN13(isbn)) { result = "good isbn"; } } catch (RemoteException e) { e.printStackTrace(); } return result; } } |
In the method validateISBN
you can see I do the web service call. Now you must be guessing how I got the stubs created. Netbeans has made that easy for us. Right click on the project and select “New JavaME Web Service Client”. Provide the WSDL URL webservices.daehosting.com/services/isbnservice.wso?WSDL and you are done.
For sake of completeness here are the other 2 classes I coded. I put the forms in two independent classes.
Class EnterISBNNumberForm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.test; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField; public class EnterISBNNumberForm extends Form { private Command okCommand; private Command exitCommand; private TextField isbnNumber; public EnterISBNNumberForm(String title, CommandListener cmdlistener) { super(title); exitCommand = new Command("Exit", Command.EXIT, 1); okCommand = new Command("Check ISBN", Command.OK, 1); isbnNumber = new TextField("ISBN#: ", "", 13, TextField.ANY); append(isbnNumber); addCommand(okCommand); addCommand(exitCommand); this.setCommandListener(cmdlistener); } public String getIsbnNumber() { return isbnNumber.getString(); } } |
Class ISBNValidatorResultForm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.test; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.StringItem; public class ISBNValidatorResultForm extends Form { private Command okCommand; private StringItem box; private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public ISBNValidatorResultForm(String title, String value, CommandListener cmdlistener) { super(title); this.value = value; okCommand = new Command("Main", Command.OK, 1); box = new StringItem("ISBN Valid => ", this.value); append(box); addCommand(okCommand); this.setCommandListener(cmdlistener); } } |
If you run the project an emulator should pop up and you can launch the application. Following two images show the application in action: