KakimotOnline

March 15, 2009

Creating a J2ME Web Service Client

Filed under: j2me, java, mobile, programming, web service — Tags: , , — nandokakimoto @ 6:13 pm

This last week I was concentrated in developing a J2ME application that communicates with a Web Service to gather some information about route planning. Basically, the mobile app ask for a route, given an origin and a destination waypoints. Although this kind of work looks very easy, I got some problems to accomplish it. Due to that, I will share here my experiences to save you some time and if you have a different solution for creating J2ME web service clients, please let me know.

First of all, you will need the following softwares:

  • NetBeans IDE: instead of my fanatism for Eclipse IDE, I used NetBeans due to its facilities in creating web services.
  • Sun Java Wireless Toolkit: java toolkit for creating mobile applications.
  • Apache Tomcat: my target Web Server. Remember to download the .zip version.

The first step is creating the Web Service. So, inside NetBeans IDE, I will configure my tomcat installation under Tools->Servers menu option. (I’m using the pt_BR version of netbeans, so the menus may be a little different). Next, I choose Add Server and type the tomcat’s path location in my local machine. (Neatbeans comes with a native Web Server called GlassFish. If you are not aware with web server now, use this configuration as default).

Later, I have to create my web application project. So, under File->New Project menu, I choose Java Web category and Web Application project type. Click on “next” until your configuration has finished. Now, it’s time to create you source. I will show here a similar implementation of my route server.

Here I show the Waypoint class.


package entities;

public class Waypoint {

    private double lat;

    private double lng;

    public Waypoint(double lat, double lng) {
        this.lat = lat;
        this.lng = lng;
    }

    public Waypoint() {
    }

    public double getLat() {
        return lat;
    }

    public void setLat(double lat) {
        this.lat = lat;
    }

    public double getLng() {
        return lng;
    }

    public void setLng(double lng) {
        this.lng = lng;
    }

}

Here I show the Route class.


package entities;

public class Route {

    private Waypoint[]  waypoints;

    public Route(Waypoint[] waypoints) {
        this.waypoints = waypoints;
    }

    public Route() {
    }

    public Waypoint[] getWaypoints() {
        return waypoints;
    }

    public void setWaypoints(Waypoint[] waypoints) {
        this.waypoints = waypoints;
    }

}

Notice that I’m using complex object and arrays, which are in the most cases the reason of a lot of problems in deploying Web Services.

Next, I create my Web Service. To do that, right-click on the package folder and choose New->WebService. Here I show the implementation of my Web Service, which just return a route created by the given latitudes and longitudes.


package webservice;

import entities.Route;
import entities.Waypoint;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author Fernando Kakimoto
 */
@WebService()
public class RouteService {

    /**
     * Operação de serviço web
     */
    @WebMethod(operationName = "getRoute")
    public Route getRoute(@WebParam(name = "oLat", partName="oLatPart") double oLat,
            @WebParam(name = "oLng", partName="oLngPart") double oLng,
            @WebParam(name = "dLat", partName="dLatPart") double dLat,
            @WebParam(name = "dLng", partName="dLngPart") double dLng) {

        Waypoint w1 = new Waypoint(oLat, oLng);
        Waypoint w2 = new Waypoint(dLat, dLng);
        Waypoint[] waypoints = new Waypoint[]{w1, w2};

        return new Route(waypoints);
    }
}

After project compilation, the project tree will be similar than the picture below:

project

The Web Service is done. It’s time to create our J2ME client. For that, there are some options. One option is using the netbeans native support for that. After created a J2ME project, inside the New->J2ME Client for Web Applications menu, you are able to select a WSDL file as source and the IDE creates the J2ME client code for you. However, I tried this approach and the generated code came with errors. So, I used the second option: Sun Java Wireless Toolkit.

The WTK offers some utilites under File->Utilities menu. One of them is the Stub Generator, which generates J2ME client code for your WSDL service. So, run your service inside NetBeans or create its respective WSDL file. Next, in the stub generator, type the file path or URL, the code destination and the CLDC version. Here is how my Stub Generator looks like.

stubgenerator

After the code generation, here is the result of my WebClient J2ME project.

projectj2me

To finish, all you have to do is instantiate RouteService_Stub class and invoke getRoute() method. To make things simple, I used just one class which corresponds to the midlet. Here is the midlet code.


package main;

import client.Route;
import client.RouteService;
import client.RouteService_Stub;
import client.Waypoint;
import java.rmi.RemoteException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.*;

/**
 * @author Fernando Kakimoto
 */
public class MainMidlet extends MIDlet implements CommandListener {
    private Command exitCommand = new Command("Exit", Command.EXIT, 1);
    private Form form = new Form("J2ME Web Client");

    public void startApp() {
        try {
            RouteService service = new RouteService_Stub();
            Route r = service.getRoute(1.5, 2.5, 1.8, 2.8);

            Waypoint origin = r.getWaypoints()[0];
            Waypoint detination = r.getWaypoints()[1];

            form.append("origin:" + origin.getLat() + "," + origin.getLng());
            form.append("\n");
            form.append("destination:" + detination.getLat() + "," + detination.getLng());

            form.addCommand(exitCommand);
            form.setCommandListener(this);
            Display.getDisplay(this).setCurrent(form);

        } catch (RemoteException ex) {
            ex.printStackTrace();
        }
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void commandAction(Command c, Displayable d) {
        if(c == exitCommand){
            destroyApp(false);
        }
    }
}

That’s it. Simple and working.
The result you can see running the midlet sample.

phone

See you,


Fernando

June 25, 2008

Internet through the sky

Filed under: mobile, news — Tags: , , — nandokakimoto @ 2:37 pm

Hi,

it’s incredible how Internet is in everywhere. After Wi-Fi, WiMax and 3G make possible the internet access in shoppings and airports, it’s time to read your favority blog inside the airplane.

According to American Airlines, their passengers will be able to test in-flight Internet access. At the beginning, just two flights will provide this service: New York – Los Angeles and San Francisco – Miami, both in Boing 7676-200. However, the company have plans to enlarge the service in two weeks.

Passengers will be able to use e-mail, instant messaging, to download video and connect to secure networks on notebook computers or other wireless devices such as smart phones through three wireless access points on the plane. Unfortunately, this service will not be free of taxs. Facing record high fuel prices, American plans to charge $9.95 to $12.95 for Internet service, depending on flight length.

Well, it seems a good entertainment alternative for that long/boring flights and to help in some late job.


Fernando

June 7, 2008

3G iPhone

Filed under: innovation, mobile, news — Tags: , , , — nandokakimoto @ 2:06 pm

Hi,

this week is far away from being an usual week for Apple Lover. In June 9th happens the Apple WWDC and a lot of speculations had been done for the secound-generation iPhone.

Apple announced the iPhone in January 9th and besides of its spectacular multi-touch screen with virtual keyboard and buttons, a big number of users claimed iPhone’s internet connection and digital camera.

Does that problems will continue in the second generation of Apple’s main product? According to gizmodo, they don’t.

To solve the internet speed, the new iPhone will come with the third generation of mobile phone standards and technology (3G), which one offers users a wider range of services while achieving greater network capacity. This means: wide-area wireless voice telephony, video calls, and broadband wireless data.

For the camera, new iPhone brings a frontal one wich allows video calls, but no more informations. So, to know if iPhone 2 will also provide users a real digital camera (video, more mega pixels, flash and better motion support) you will have to wait. And so do I.

So, let’s wait June 9th and see another Steve’s show.


Fernando

March 8, 2008

Choose the playlist with Smart Party

Filed under: mobile, wi-fi — nandokakimoto @ 6:23 pm

dj.jpg

Is the party boring? How about listen to musics that you have in your player?

That’s the purpose of a project in California University, wich idea is allow people to become Dj by using their mobile phone, notebook and players to choose the party’s playlist through Wi-Fi.

For that, the guests must install a software in their players responsable to look for music installed and transmit them to a computer connected with the sound boxes. Thus, the software make statistics of what is the most popular musics at the moment and create the playlist.

The software can also personalize the playlist according the different party’s environment, by controlling guests movement and choosing the musics for each environment. So, if the heavy metal group goes to the kitchen, the pop group may listen their song at the living-room.

The Smart Party’s project is just at prototyping fase and you can see more details here.


Fernando

March 6, 2008

Digital Television and Mobile Phones

Filed under: digital television, mobile — nandokakimoto @ 11:52 pm

samsung-aberto-01.jpg

On March 4th, Samsung announced the first mobile with a integrated digital television receptor. This new gadget will be fabricated on Brazil and sold initially by Vivo.

The V820L costs between R$1.600 and R$1.800 depending on the operator and the plan choosen by the consumer. Besides receive the digital television signal, it is a 3G model with a 2.0 MP camera, OLED screen, bluetooth and expandable memory.

This is one more option for those who works with digital television, just like me, providing a new environment to write applications and services for this kind of technology. Although, the digital television in Brazil is just at the beginning with only 50 thousands converters sold. So, we will have to wait (I don’t know for how long) the brazilian digital television system enhance its area’s transmition and become popular, when some work maybe appear for us.

I hope that it doesn’t take too much time to happen.


Fernando

February 22, 2008

While Microsoft attempts buying the Yahoo!, Google maybe buy…

Filed under: google, mobile, news — nandokakimoto @ 1:15 am

While the whole world look at the Microsoft’s attempts to buying Yahoo!, the Google, investing in wireless devices, maybe will buy a balloon company.

Yeah, you didn’t read wrong and I will repeat: Google may buy a Balloon Company.

According to the Wall Street Journal, the idea of use balloons to provide wireless service to millions of rural Americans has caught the eye of Google Inc.

More information on http://online.wsj.com/public/article/SB120347353988378955.html?mod=blog

There is also a nice video demostrating this experience.


Fernando

Older Posts »

Blog at WordPress.com.