J2ME和Web Service-KSOAP的羅曼史
----J2ME和Web Service-KSOAP的快速上手
作者:Cleverpig版權(quán)聲明:能夠任意轉(zhuǎn)載,,轉(zhuǎn)載時(shí)請(qǐng)務(wù)必以超鏈接形式標(biāo)明文章原始出處和作者信息及本聲明
原文地址:
http://www./resource/article/43/43662_J2ME_KSOAP.Html
關(guān)鍵詞: j2me soap webservice
1,、服務(wù)端這次要發(fā)布的web service很簡(jiǎn)單,。他的功能是把從客戶端傳入的字符串中的小寫(xiě)字母轉(zhuǎn)變成大寫(xiě)字母,,再返回給客戶端,。
Soap 服務(wù)器采用apache的AXIS(能夠從http://ws./axis/下載),,應(yīng)用服務(wù)器能夠選用各種servlet 容器,,我這里采用的是weblogic,。
1.1 實(shí)現(xiàn)類的源代碼:
// StringPRocessor.java
package com.jagie.j2me.ws;
public class StringProcessor
{
public StringProcessor()
{
}
public String process(String name)
{
return name.toUpperCase();
}
}
1.2 發(fā)布步驟 1.準(zhǔn)備一個(gè)目錄作為web
application的發(fā)布目錄,我這里的這個(gè)目錄叫jagiews,這個(gè)目錄的全路徑中最好不要有空格和中文,。我的發(fā)布目錄結(jié)構(gòu)如下:
2.編譯StringProcessor.java,把生成的StringProcessor.class置于: /jagiews/WEB-INF/classes/com/jagie/j2me/ws目錄下。
3.在jagiews/WEB-INF/lib 文檔夾中置入以下axis服務(wù)器需要的jar文檔 axis.jar,,axis-ant.jar,,commons-discovery.jar,,commons-logging.jar,jaxrpc.jar,,log4j-1.2.8.jar,,saaj.jar ,wsdl4j.jar,。這些文檔能夠在http://ws./axis/下載,。
4.在jagiews/WEB-INF目錄下增加2個(gè)發(fā)布描述文檔:
server-config.wsdd,web.
xml,。
#server-config.wsdd
<?xml version="1.0" encoding="UTF-8"?>
<deployment xmlns=
"http://xml./axis/wsdd/"
xmlns:java=
"http://xml./axis/wsdd/providers/java">
<globalConfiguration>
<parameter name=
"adminPassWord" value="admin"/>
<parameter name=
"attachments.Directory"
value="C:/Program Files
/Apache Tomcat 4.0/webapps
/axis/WEB-INF/attachments"/>
<parameter name=
"attachments.implementation"
value="org.apache.axis.
attachments.AttachmentsImpl"/>
<parameter name=
"sendXsiTypes"
value="true"/>
<parameter name=
"sendMultiRefs"
value="true"/>
<parameter name=
"sendXMLDeclaration"
value="true"/>
<parameter name=
"axis.sendMinimizedElements"
value="true"/>
<requestFlow>
<handler type=
"java:org.apache.axis.handlers.JWSHandler">
<parameter
name="scope"
value="session"/>
</handler>
<handler type=
"java:org.apache.axis.handlers.JWSHandler">
<parameter
name="scope"
value="request"/>
<parameter
name="extension"
value=".jwr"/>
</handler>
</requestFlow>
</globalConfiguration>
<handler name=
"LocalResponder"
type="java:org.apache.axis.
transport.local.LocalResponder"/>
<handler name="URLMapper"
type="java:org.apache.axis.
handlers.http.URLMapper"/>
<handler name="RPCDispatcher"
type="java:org.apache.axis.
providers.java.RPCProvider"/>
<handler name="Authenticate"
type="java:org.apache.axis.
handlers.SimpleAuthenticationHandler"/>
<handler name="MsgDispatcher"
type="java:org.apache.axis.
providers.java.MsgProvider"/>
<service name="AdminService"
provider="java:MSG">
<parameter name="allowedMethods"
value="AdminService"/>
<parameter name="enableRemoteAdmin"
value="false"/>
<parameter name="className"
value="org.apache.axis.utils.Admin"/>
<namespace>
http://xml./axis/wsdd/
</namespace>
</service>
<service name="Version"
provider="java:RPC">
<parameter name="allowedMethods"
value="getVersion"/>
<parameter name="className"
value="org.apache.axis.Version"/>
</service>
<!-- your service begin-->
<service name="StringProcess"
provider="java:RPC">
<parameter name="allowedMethods"
value="process"/>
<parameter name="className"
value="com.jagie.j2me.
ws.StringProcessor"/>
</service>
<!-- your service end -->
<transport name="http">
<requestFlow>
<handler type="URLMapper"/>
<handler type=
"java:org.apache.axis.handlers.
http.HTTPAuthHandler"/>
</requestFlow>
</transport>
<transport name="local">
<responseFlow>
<handler type="java:org.apache.axis.
transport.local.LocalResponder"/>
</responseFlow>
</transport>
</deployment>
# web.xml
<?xml version="1.0"
encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.3//EN"
"http://java.
/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<display-name>Apache-Axis</display-name>
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis
Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</servlet>
<servlet>
<servlet-name>AdminServlet</servlet-name>
<display-name>Axis Admin Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AdminServlet
</servlet-class>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet>
<servlet-name>SOAPMonitorService</servlet-name>
<display-name>SOAPMonitorService</display-name>
<servlet-class>
org.apache.axis.monitor.SOAPMonitorService
</servlet-class>
<init-param>
<param-name>SOAPMonitorPort</param-name>
<param-value>5001</param-value>
</init-param>
<load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>SOAPMonitorService</servlet-name>
<url-pattern>/SOAPMonitor</url-pattern>
</servlet-mapping>
<!-- uncomment this if you want the admin servlet -->
<!--
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/servlet/AdminServlet
</url-pattern>
</servlet-mapping>
-->
<!-- currently the W3C havent settled
on a media type for WSDL;
http://www./TR/2003/
WD-wsdl12-20030303/#ietf-draft
for now we go with the basic
'it's XML' response -->
<mime-mapping>
<extension>wsdl</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
<mime-mapping>
<extension>xsd</extension>
<mime-type>text/xml</mime-type>
</mime-mapping>
</web-app>
5.開(kāi)啟您的application server,,把目錄jagiews發(fā)布為一個(gè)名叫jagiews的web application。
6.測(cè)試:打開(kāi)瀏覽器,,輸入網(wǎng)址(這里使用的是weblogic,其他的服務(wù)器請(qǐng)酌情修改): http://localhost:7001/jagiews/services/StringProcess?method=process&name=
QQqq,,假如瀏覽器能在返回的xml文檔中顯示字符串"QQQQ",恭喜您,,您的web service發(fā)布成功了,。假如發(fā)布不成功,請(qǐng)按以上發(fā)布步驟檢查一下,。
2,、客戶端 客戶端自然是用MIDlet了,但是用什么方式來(lái)訪問(wèn)web service呢,?其實(shí)有3種訪問(wèn)方式
直接用HttpConnection訪問(wèn) http://localhost:7001/jagiews/services/StringProcess?method=process&name=qqqq,,得到xml的返回?cái)?shù)據(jù),然后用kxml(http://kxml./)解析,,得到返回值,。
假如您的手機(jī)支持MIDP2.0的話,能夠考慮使用JSR172,。
用ksoap api,。
這里講述第三種方式。使用之前,,您需要從 http://ksoap./software/downloads/index.html下載穩(wěn)定的ksoap包,,置于您的classpath中。
2.1 客戶端源代碼 2.1.1 WSClientMIDlet.java
package com.jagie.j2me.ws;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c)
2004</p>
* <p>Company: </p>
* @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.notifyDestroyed();
instance = null;
}
}
2.1.2 DisplayForm.java
package com.jagie.j2me.ws;
import javax.microedition.lcdui.*;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c)
2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class DisplayForm
extends Form
implements CommandListener,
Runnable
{
private TextField textField1;
private Thread t;
public DisplayForm()
{
super("字符轉(zhuǎn)換webservice測(cè)試");
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
("待處理的字符串是:");
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("處理后的字符串是:", s2);
this.append(resultItem);
}
}
2.1.3 StringProcessorStub.java
package com.jagie.j2me.ws;
import org.ksoap.*;
import org.ksoap.transport.HttpTransport;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @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/jagiews/services/StringProcess",
"process");
rpc.addProperty("name", name);
HttpTransport ht =
new HttpTransport
("http://localhost:7001/
jagiews/services/StringProcess",
"");
result = (String) ht.call(rpc);
}
catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
總結(jié) 有了ksoap,,手機(jī)上調(diào)用web service就很容易了,。但是要注意的是,使用網(wǎng)絡(luò)連接這種費(fèi)時(shí)操作的時(shí)候,,一定要單獨(dú)開(kāi)線程進(jìn)行,,不要直接寫(xiě)在commandAction()方法里,否則出現(xiàn)畫(huà)面被鎖住的情況,。進(jìn)入討論組討論,。