This tutorial describes how you can use NetBeans IDE to create a mobile client application that consumes a web service. You will create an application that fetches the daily Dilbert comic strip via a live on-line web service, converts the graphics to a space-saving format, and displays the strip on a mobile device.
In this tutorial, you first create a web service client in a mobile application to connect to the on-line DailyDilbert service. You are then shown how to use the Visual MIDlet Designer to graphically design the layout of your application. Next, you add code that transforms the comic's graphic format from GIF to PNG, which is the format that mobile devices support. Finally, you use an emulator to see the daily strip in the IDE.
Contents |
Before you begin, you need to download or install the following software on your computer:
First, we'll create a new Web Application project. The project will contain our web service client.
The application is created and looks as follows in the Projects window:
Now we'll add a web service client to consume the DailyDiblert service.
The Web Service Client wizard appears.
http://www.esynaps.com/WebServices/DailyDiblert.asmx?WSDL
The IDE downloads the WSDL file and creates the client-side artifacts, which you can see in the build folder, within the Files window. The Projects window displays the logical view as shown here:
In this section, we'll create a new Mobile Application project. We then use the Mobile Client to Web Application wizard to connect the application to the DailyDiblert service.
Click Next.
Note:
When you click Install SDK/Platform/Emulator, you see the following dialog, where you should click Java ME MIDP Platform Emulator:
Complete the wizard to install the emulator.
The mobile application is created and looks as follows in the Projects window:
The Mobile Client to Web Application wizard generates a servlet that connects to a web application that includes a web service client.
Click Next.
DailyDiblert.asmx
, as shown here:
Click Next.
Click Next.
The new artifacts are created and the Projects window now displays the application's logical view, as below:
In this section, you will create a visual MIDlet and use the designer to add components and connect them to each other.
Click Finish.
A new visual MIDlet file is created and is opened in the Visual MIDlet Designer.
You can put the components anywhere you like on the designer.
In the next section, we will create a new component and add it to the palette. Then we will drag it onto the designer. Next, we will connect the components to each other to establish the relationships between them.
In this section, we'll create a custom canvas component to display the graphic file.
Click Next.
package dilbert; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; public class DilbertCanvas extends Canvas { private int coordX, coordY; private int imageWidth, imageHeight; private Image image; private int screenWidth, screenHeight; /** Creates a new instance of DilbertCanvas */ public DilbertCanvas() { screenHeight = getHeight(); screenWidth = getWidth(); } public void setImage(Image image) { this.imageWidth = image.getWidth(); this.imageHeight = image.getHeight(); this.image = image; } protected void paint(Graphics graphics) { graphics.setColor(255, 255, 255); graphics.fillRect(0, 0, getWidth(), getHeight()); graphics.drawImage(image, coordX, coordY, Graphics.LEFT | Graphics.TOP); } protected void keyPressed(int keyCode) { int key = getGameAction(keyCode); if (key == DOWN) { if (coordY - screenHeight + imageHeight > 0) { coordY -= 10; } } else if (key == UP) { if (coordY < 0) { coordY += 10; } } else if (key == RIGHT) { if (coordX - screenWidth + imageWidth > 0) { coordX -= 10; } } else if (key == LEFT) { if (coordX < 0) { coordX += 10; } } repaint(); } }
In the next section, we add the component to the palette and then we drag it from there and drop it on the designer.
Next, you add the canvas component to the palette and then you incorporate the canvas component into the application.
Click Next.
dilbert.DilbertCanvas
.
Click Finish.
In the Navigator (Ctrl-7), you should now see the following:
At this point, you should see the following:
getTask()
method as shown below in bold:private byte[] dailyDilbertImage_returnValue; public SimpleCancellableTask getTask() { if (task == null) { // write pre-init user code here task = new SimpleCancellableTask(); task.setExecutable(new org.netbeans.microedition.util.Executable() { public void execute() throws Exception { if (dailyDilbertImage_returnValue == null) { DilbertViewer client = new DilbertViewer(); dailyDilbertImage_returnValue = client.dailyDilbertImage(); Image dilbertImage = Image.createImage(dailyDilbertImage_returnValue, 0, dailyDilbertImage_returnValue.length); getDilbertCanvas().setImage(dilbertImage); } } }); // write post-init user code here } return task; }
Now we have a client application to receive the daily image. But the received image is in GIF format, which is much too large for mobile devices with limited memory. So we'll need to transform the image to the PNG format, which is smaller and more easily sized for the many different display sizes available to mobile devices.
DailyDilbert_Proxy
file, shown below:
dailyDilbertImage()
method by adding the highlighted code below: public byte[] dailyDilbertImage() throws RemoteException, Exception { try { ByteArrayInputStream in = new ByteArrayInputStream(getService().getDailyDilbertSoap().dailyDilbertImage()); BufferedImage image = ImageIO.read(in); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, "png", out); return out.toByteArray(); } catch (java.rmi.RemoteException ex) { throw ex; } catch (Exception ex) { throw ex; } }
First we deploy the web application. Then we run the mobile application.
This tutorial showed how you can use the Mobile Client to Web Application to quickly create a MIDlet that consumes a Web service.
It also demonstrated how you can create a custom component and add it to your application using the Visual Mobile Designer.In addition to the IDE's built-in help documentation about Java ME CDC development, tutorials and articles about the technologies supported by the Mobility pack can be found at the following location: