Quick Start With j2-webservice-KSOAP

Source: Internet
Author: User
Tags apache tomcat
The Quick Start of j2s and WebService-KSOAP-Linux general technology-Linux programming and kernel information. For more information, see the following. This article uses a concise example to illustrate how to use KSOAP APIs on mobile phones to access Web services on local servers. The detailed operation steps and all the Server and Client source code are provided.

1. Server

The web service to be released this time is very simple. The function is to convert lowercase letters from the strings passed in by the client into uppercase letters and then return them to the client. The Soap server uses the apache AXIS (which can be downloaded from http://ws.apache.org/axis/). The application server can use various servlet containers. weblogic is used here.

1.1 source code of the implementation class

// StringProcessor. java
Package com. jarie. j2-ws;

Public class StringProcessor {
Public StringProcessor (){
}

Public String process (String name ){
Return name. toUpperCase ();
}

}

1.2 Release steps

1. Prepare a directory to serve as the release directory of the web application. Here, the directory is called jariews. It is best not to have blank spaces or Chinese characters in the full path of this directory. My release directory structure is as follows:



2. Compile StringProcessor. java and place the generated StringProcessor. class in the directory \ jariews \ WEB-INF \ classes \ com \ jarie \ j2_ws.

3. in the jariews \ WEB-INF \ lib folder, set the jar file required by the following axis server to axis. jar, axis-ant.jar, commons-discovery.jar, commons-logging.jar, jaxrpc. jars, log4j-1.2.8.jar, saaj. jar, wsdl4j. jar. These files can be downloaded at http://ws.apache.org/axis ,:



4. Add 2 release description files under the jariews \ WEB-INF Directory: server-config.wsdd, web. xml.

# Server-config.wsdd


Xmlns: java = "http://xml.apache.org/axis/wsdd/providers/java">



Value = "org. apache. axis. attachments. AttachmentsImpl"/>














Type = "java: org. apache. axis. transport. local. LocalResponder"/>
Type = "java: org. apache. axis. handlers. http. URLMapper"/>
Type = "java: org. apache. axis. providers. java. RPCProvider"/>
Type = "java: org. apache. axis. handlers. SimpleAuthenticationHandler"/>
Type = "java: org. apache. axis. providers. java. MsgProvider"/>





Http://xml.apache.org/axis/wsdd/








Value = "com. jarie. j2-ws. StringProcessor"/>















# Web. xml



PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.3 // EN"
Http://java.sun.com/j2ee/dtds/web-app_2.2.dtd>


Apache-Axis

AxisServlet
Apache-Axis Servlet

Org. apache. axis. transport. http. AxisServlet




AdminServlet
Axis Admin Servlet

Org. apache. axis. transport. http. AdminServlet

100



SOAPMonitorService
SOAPMonitorService

Org. apache. axis. monitor. SOAPMonitorService


SOAPMonitorPort
5001

100



AxisServlet
/Servlet/AxisServlet



AxisServlet
*. Jws



AxisServlet
/Services /*



SOAPMonitorService
/SOAPMonitor







Wsdl
Text/xml


Xsd
Text/xml



5. Enable your application server and publish the directory "jagiews" as a web application named "jagiews.

6. test: Open the browser and enter the URL (weblogic is used here, and other servers should be modified as appropriate): http: // localhost: 7001/jagiews/se... d = process & name = qqqq. If the browser can display the string "QQQQ" in the returned xml document, congratulations! Your web service has been published successfully. If the release fails, follow the above steps to check the release.

2. Client

The client uses the MIDlet, but how does it access the web service? There are actually three access methods

Use HttpConnection directly to access http: // localhost: 7001/jaiews/se... d = process & name = qqqq, get the xml returned data, and then use kxml (http://kxml.enhydra.org/) parsing, get the return value.
If your mobile phone supports MIDP2.0, you can use JSR172.
Use ksoap APIs.
The third method is described here. Before use, you need.

2.1 Client source code

2.1.1 WSClientMIDlet. java

Package com. jarie. j2-ws;

Import javax. microedition. midlet .*;
Import javax. microedition. lcdui .*;

/**
*

Title:


*

Description:


*

Copyright: Copyright (c) 2004


*

Company:


* @ Author not attributable
* @ Version 1.0
*/

Public class WSClientMIDlet
Extends MIDlet {
Static WSClientMIDlet instance;

Public WSClientMIDlet (){
Instance = this;
}

Public void startApp (){
Display display = Display. getDisplay (this );
DisplayForm displayable = new DisplayForm ();
Display. setCurrent (displayable );

}

Public void pauseApp (){
}

Public void destroyApp (boolean unconditional ){
}

Public static void quitApp (){
Instance. destroyApp (true );
Instance. policydestroyed ();
Instance = null;
}

}

2.1.2 DisplayForm. java

Package com. jarie. j2-ws;

Import javax. microedition. lcdui .*;


/**
*

Title:


*

Description:


*

Copyright: Copyright (c) 2004


*

Company:


* @ Author not attributable
* @ Version 1.0
*/

Public class DisplayForm
Extends Form
Implements CommandListener, Runnable {
Private TextField textField1;
Private Thread t;

Public DisplayForm (){
Super ("character conversion webservice test ");

Try {
JbInit ();
}
Catch (Exception e ){
E. printStackTrace ();
}


}

Private void jbInit () throws Exception {
// Set up this Displayable to listen to command events
TextField1 = new TextField ("", "", 15, TextField. ANY );
This. setCommandListener (this );
TextField1.setLabel ("the string to be processed is :");
TextField1.setConstraints (TextField. ANY );
TextField1.setInitialInputMode ("Tester ");
SetCommandListener (this );
// Add the Exit command
AddCommand (new Command ("Exit", Command. EXIT, 1 ));
AddCommand (new Command ("Process", Command. OK, 1 ));
This. append (textField1 );
}

Public void commandAction (Command command, Displayable displayable ){

If (command. getCommandType () = Command. EXIT ){
WSClientMIDlet. quitApp ();
}
Else if (command. getCommandType () = Command. OK ){
T = new Thread (this );
T. start ();
}
}

Public void run (){
String s1 = textField1.getString ();
String s2 = new StringProcessorStub (). process (s1 );
StringItem resultItem = new StringItem ("the processed string is:", s2 );
This. append (resultItem );

}

}

2.1.3 StringProcessorStub. java

Package com. jarie. j2-ws;

Import org. ksoap .*;
Import org. ksoap. transport. HttpTransport;

/**
*

Title:


*

Description:


*

Copyright: Copyright (c) 2004


*

Company:


* @ Author not attributable
* @ Version 1.0
*/

Public class StringProcessorStub {
Public StringProcessorStub (){
}

Public String process (String name ){
String result = null;
Try {

SoapObject rpc = new SoapObject
("Http: // localhost: 7001/jariews/services/StringProcess", "process ");

Rpc. addProperty ("name", name );

HttpTransport ht = new HttpTransport
("Http: // localhost: 7001/jariews/services/StringProcess ","");

Result = (String) ht. call (rpc );

}
Catch (Exception e ){
E. printStackTrace ();
}

Return result;

}
}

Test Client

Now, try to run WSClientMIDlet in your ide. If the call is successful, the following screen appears:



Summary

With ksoap, it is easy to call web service on a mobile phone. However, when using a network connection for time-consuming operations, you must open a thread separately. Do not write it directly in the commandAction () method; otherwise, the screen will be locked.

References

Http://ksoap.enhydra.org/softwar... StockQuoteDemo. java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.