Java ME to Java EE Application - Consuming Webservice on Mobile Device
By lukas on Jan 06, 2007
ScenarioThe scenario of the demo was as simple as possible. I wanted to create a mobile application that connect to a webservice and displays the result from the webservice back on the phone. The application had to be able to run on a phone with support for Webservices API (JSR172) but even on a phone without the support for JSR172. The most of devices on market are without the JSR172 today.
WebserviceI choose a simple public webservice that converts a number into words (e.g. 123 => one hundred twenty three). It's really simple webservice - its argument is a complex type of five strings and a string as return value.
JSR 172 ClientAs you can see on the picture bellow the mobile client communicates directly with the webservice. The communication is maintained by JAX-RPC runtime on the device. It works only on devices with the JSR172.
At first I created new mobile application and generated the client to the webservice with New | J2ME Web Service Client wizard.
I needed to specify only the WSDL of the webservice. The wizard generates everything for me. I pressed the Retrive WSDL button and then Finish. It generates stubs and a Sample MIDlet. When you look at the generated Sample MIDlet you can understand how to use the generated stubs. The sample midlet isn't usually "good enough" to be used in a real application. It is useful to check the webservice and to have idea what is it all about.
Note:The sample midlet isn't generated when there are arrays or other types that are not easy to visualize in the webservice.
It was the easier part of the demo... The wizard did everything for me.
Mobile Client to Web ApplicationUnfortunately this quick solution will not work on a phone that doesn't support Webservices API. NetBeans Mobility Pack will help you to create an application even for these phones. We will have to create a webservices client for the webservice in a web project first. Then we will use this webservice client for generating of a midlet client. The IDE will generate server part and client part for you. The client part will be deployed on your device (phone) and will communicate with the server part that is a middle tier between the mobile client and the webservice. The mobile client will communicate with the webservice client through binary protocol. You don't have to care about the communication at all. It will be generated for you.
Now you know what is the difference between these two solutions.
Let me describe this step in more details. You have to create the middle tier first. Create new webservice client in a web project. Use New | Web Service Client on the web project. Just specify the WSDL of the webservice and let IDE to generate the webservice client. Please note: the webservice client has to be the JAX-RPC one. It's possible to create JAX-RPC clients only in J2EE 1.4 web project. Don't forget to set up the Java EE Version to J2EE 1.4 when you are creating the web project.
When you have the webservice client created you can invoke New | Mobile Client to Web Application wizard that will generate the server and client part of the communication.
Select the web project where is the webservice client, choose a package where the server part will be generated. Use Web Service Client in Web Application radio button and in the combo box select the webservice client. On next panel choose what methods you need from the webservice client. On the last panel specify a name, package of the mobile client part. Let it generate!
Add Preprocessor Blocks + Combine Them TogetherThe generated code is very similar in both cases (by purpose). I generated both solutions into one directory. I can easily use device fragmentation solution that the Mobility Pack provides and merge the both solution into one source base. I used preprocessor blocks with abilities together with filtering of classes in jar file to achieve this.
I created two configurations in the mobile project. One is with the JSR 172 support and second isn't. I can build distribution jar(jad) for or every configuration separately. I only changed the client used in one of the generated sample MIDlets. It doesn't matter which one you'll choose. The methods for connecting to the webservice are generated the same for both clients. Because of the preprocessor block one client is used in distribution jar for the emulator platform with the JSR172 and the different one for the emulator platform without the JSR172. It is caused by the JSR172 ability in the emulator platform configuration. The code looks like
//#ifdef JSR172 private ConvertorSoap_Stub client = new ConvertorSoap_Stub(); //#else //# private WebToMobileClient client = new WebToMobileClient(); //#endifThat's the only change that I had to do to the client (midlet). When I compile and run the application in different configuration then the right client is used regarding on JSR172 ability in the project properties of the active configuration. The JSR172 ability is created automatically in the configuration if there is Webservices API available or you can add/remove it manually in the project properties.
To be able to compile the application even in the configuration for device platform without JSR172 we have to filter out the classes using JSR172. It can be easily done in project properties.
Then remove the unused MIDlet from the list of the MIDlets in project properties of DefaultConfiguration.
In the demo I changed a little bit the UI of the generated sample MIDlet. I doesn't need to be described here. It is obvious from the demo...
TipsThere are some hidden things that you won't see in the demo. You need to set the environment a little bit to be able to create and run the application.
- Set up your proxies if it's needed
- It can happen that your the connection fails for JSR172. The server returns "Missing end tag for Body or Envelope". It's WTK bug that can be easily work arounded. Switch the WTK to trusted mode (or use the latest WTK 2.5 where the bug is fixed).
- Flash Demo for SDN Channel
- Voice Transcript of the Demo
- WSDL of the Convertor Webservice
- Sample projects - MobileClient and WebApp2ConvertorWS. One is the proxy project with the webservice client. The MobileClient is whole j2me application with both -JSR172/no JSR172- solutions. Please note that WebApp2ConvertorWS has to be deployed when you are running MobileClient in DefaultGrayPhone_NOJSR172 configuration.