You Are Here:

Community: Wiki

This page was last modified on 30 September 2009, at 19:09.

Make EJB client in Java ME

From Forum Nokia Wiki


Reviewer Approved   

We will create a small EJB application and we have two way to solve the communication from the mobile phone.

Contents

EJB application

The appliaction wait for a string and send an answer: "Hello $string!"

package com.nokia.forum.wiki;
 
import javax.ejb.Stateless;
import javax.jws.WebService;
 
@Stateless()
@WebService
public class Hello
{
public String getHello(String name)
{
return "Hello " + name + "!";
}
}

Easy way with WSA and NetBeans

If the target phone support the WSA we only need to import the WSDL file from the server and the NetBeans wizard will create a sample application to us.

Steps:

  • Create a new Mobile Application
  • Right click on the project / New / J2ME Web Service Client
  • Write the URL to the WSDL URL box and click on the Retrieve WSDL button
  • Check on the Create Sample MIDLet and click on the Finish

Image:nbhw1.pngImage:nbhw2.pngImage:nbhw3.png

Other way without WSA

If the target phone doesn't support the WSA feature we need to use some alternative way, like kSOAP2. We can't use the benefits of the WSDL and we need edit manually the soap message.

Don't forget to add the libs of the kSOAP2 to the project!

Image:soap1.pngImage:soap2.png

package helloworld;
 
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.*;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransport;
 
public class HelloWorldMidlet extends MIDlet implements CommandListener {
 
public HelloWorldMidlet() {
}
// Display
private Display display;
// Main form
private Form form;
// For the message
private StringItem stringItem;
// For the exit command
private Command exitCommand;
 
public void commandAction(Command command, Displayable displayable) {
if (displayable == form) {
if (command == exitCommand) {
exitMIDlet();
}
}
}
 
public void startApp() {
// Create form
stringItem = new StringItem("Hello", "Hello World!");
form = new Form(null, new Item[] {stringItem});
exitCommand = new Command("Exit", Command.EXIT, 1);
form.addCommand(exitCommand);
form.setCommandListener(this);
 
// Get display for drawning
display = Display.getDisplay(this);
display.setCurrent(form);
soapCall();
}
 
public void pauseApp() {
}
 
public void destroyApp(boolean unconditional) {
}
 
public void exitMIDlet() {
display.setCurrent(null);
destroyApp(true);
notifyDestroyed();
}
public void soapCall() {
SoapSerializationEnvelope soapEnvelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
// Create your outgoing request
SoapObject soapObject =
new SoapObject("http://wiki.forum.nokia.com/", "getHello");
// Add a parameter
soapObject.addProperty("arg0", "Tibor");
// Assigns the object to the envelope
soapEnvelope.setOutputSoapObject(soapObject);
// Make the call
try {
HttpTransport transport =
new HttpTransport("http://localhost:8080/HelloService/Hello");
transport.call(null , soapEnvelope);
Object responseObject = null;
responseObject = soapEnvelope.getResponse();
System.out.println(responseObject);
stringItem.setText(responseObject.toString());
}
catch (Exception e){
System.out.println("Exception : "+ e.getMessage());
e.printStackTrace();
}
}
}

Usefull links

kSOAP2

NetBeans

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fE52eadingE5faE5fE44ME5fAdapterE5fE44E44FX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZenterpriseQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZuserE5ftagQSxenterpriseX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx5E2E0000X