Sun Java Solaris Communities My SDN Account
 
Code sample

Using a J2ME Client to Access a Web Service

 
Function
Many developers want to access Web services from their MIDP client applications that run on mobile phones. The current MIDP specification does not contain facilities for using SOAP, although JSR 172 promises to provide this functionality soon.
 
Until then, the kSOAP open source product has been a popular way to bridge the gap. In this sample, developers learn how to integrate kSOAP with the J2ME Wireless Toolkit and run a sample application that will prompt for a stock symbol and display the stock price.
 
Topics Covered
In this example, a MIDlet queries the user for a stock symbol and calls a Web service to get that symbol's stock price. This sample shows the developer how to:
 
  • Create a MIDlet project using the J2ME Wireless Toolkit.
  • Add libraries, modify the source code, and build the MIDlet project.
  • Use kSOAP to call a SOAP-RPC Web service that takes an integer parameter and returns a string.
 

Back to top

Requirements
This sample requires the following product:
 
 

Back to top

Source Files
Instructions for downloading all the source code for the StockQuoteDemo appear below in the Instructions section.
 
The following code excerpt illustrates the logic that actually makes the SOAP call:
 
//Create a SoapObject by specifying the 
//URN and the method name of the SOAP
//RPC web service.
SoapObject rpc = new SoapObject 
(&urn:xmethods-delayed-quotes&, &getQuote&);

//The addProperty method allows you to specify
// parameters to for the method used.
rpc.addProperty (&symbol&, symbol);

//The HttpTransport class can be used to 
// make the actual call. It's constructor
// accepts the Web service end point as
// well as the method to be called.
resultItem.setText (&&+new HttpTransport 
(&http://services.xmethods.net/soap&,
&urn:xmethods-delayed-quotes#getQuote&).call (rpc));
 

Back to top

Instructions
Follow these steps to run the StockQuoteDemo application:
 
  • Install the J2ME Wireless Toolkit, 1.0.4.
  • Start the J2ME Wireless Toolkit application (&KToolbar&) from your &Start& menu.
  • Click &New Project&.
  • Enter StockQuote and StockQuoteDemo for &Project Name& and &MIDlet Class Name& fields.
  • Click &Create Project&.
  • Select &OK& to accept the settings for the new project.
  • Note that the console window now shows the directories that should contain the source, resource, and library files for the application.
  • Download the latest version of the kSOAP MIDP JAR file and put it in the directory specified on the console of the toolkit as the &library directory&. This directory should be: <INSTALLDIR>appsStockQuotelib.
  • Download the latest version of the kXML JAR file and put it in the same directory as the previous step. This is a library file that is used by kSOAP.
  • Download the StockQuoteDemo.java sample MIDlet and put it in the directory specified on the console of the toolkit as the source directory. This directory should be: <INSTALLDIR>appsStockQuotesrc.
  • If the org.ksoap.http.* path is imported at the top of the StockQuoteDemo.java code sample, change that path to import org.ksoap.transport.*. Note that this is necessary because the HttpTransport class has moved locations within the kXML library.
  • Click the &Build& button appearing on the J2ME Wireless Toolkit window. You should see a message that the build completed successfully.
  • Click the &Run& button. Now use the mobile phone emulator to specify the stock symbol to look up.
 

Back to top

See Also
Wireless Developer
(http://wireless.java.sun.com)
 
J2ME Archives
(http://www.billday.com/j2me/)
 
kSOAP Project
(http://kSOAP.org)
 

Back to top

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.

Oracle is reviewing the Sun product roadmap and will provide guidance to customers in accordance with Oracle's standard product communication policies. Any resulting features and timing of release of such features as determined by Oracle's review of roadmaps, are at the sole discretion of Oracle. All product roadmap information, whether communicated by Sun Microsystems or by Oracle, does not represent a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. It is intended for information purposes only, and may not be incorporated into any contract.