摘要
本文用一个简明的例子,阐述了如何在手机上用KSOAP API来访问本地服务器上的Web Service。并且给出了详细的操作步骤和全部的Server,Client源代码。
1. 服务端
这次要发布的web service非常简单。它的功能是把从客户端传入的字符串中的小写字母转变成大写字母,再返回给客户端。Soap 服务器采用apache的AXIS(可以从http://ws.apache.org/axis/下载),应用服务器可以选用各种servlet 容器,我这里采用的是weblogic。
1.1 实现类的源代码
// StringProcessor.java
package com.jagie.j2me.ws;
public class StringProcessor {
public StringProcessor() {
}
public String process(String name){
return name.toUpperCase();
}
}
1.2 发布步骤
1.准备一个目录作为web application的发布目录,我这里的这个目录叫jagiews,这个目录的全路径中最好不要有空格和中文。我的发布目录结构如下:
2.编译StringProcessor.java,把生成的StringProcessor.class置于: \jagiews\WEB-INF\classes\com\jagie\j2me\ws目录下。
3.在jagiews\WEB-INF\lib 文件夹中置入以下axis服务器需要的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.apache.org/axis/下载,如图所示:
4.在jagiews\WEB-INF目录下增加2个发布描述文件:server-config.wsdd,web.xml。
#server-config.wsdd
<?xml version="1.0" encoding="UTF-8"?
<deployment xmlns="http://xml.apache.org/axis/wsdd/"
xmlns:java="http://xml.apache.org/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"/
<namespacehttp://xml.apache.org/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.sun.com/j2ee/dtds/web-app_2.2.dtd"
<web-app
<display-nameApache-Axis</display-name
<servlet
<servlet-nameAxisServlet</servlet-name
<display-nameApache-Axis Servlet</display-name
<servlet-class
org.apache.axis.transport.http.AxisServlet
</servlet-class
</servlet
<servlet
<servlet-nameAdminServlet</servlet-name
<display-nameAxis Admin Servlet</display-name
<servlet-class
org.apache.axis.transport.http.AdminServlet
</servlet-class
<load-on-startup100</load-on-startup
</servlet
<servlet
<servlet-nameSOAPMonitorService</servlet-name
<display-nameSOAPMonitorService</display-name
<servlet-class
org.apache.axis.monitor.SOAPMonitorService
</servlet-class
<init-param
<param-nameSOAPMonitorPort</param-name
<param-value5001</param-value
</init-param
<load-on-startup100</load-on-startup
</servlet
<servlet-mapping
<servlet-nameAxisServlet</servlet-name
<url-pattern/servlet/AxisServlet</url-pattern
</servlet-mapping
<servlet-mapping
<servlet-nameAxisServlet</servlet-name
<url-pattern*.jws</url-pattern
</servlet-mapping
<servlet-mapping
<servlet-nameAxisServlet</servlet-name
<url-pattern/services/*</url-pattern
</servlet-mapping
<servlet-mapping
<servlet-nameSOAPMonitorService</servlet-name
<url-pattern/SOAPMonitor</url-pattern
</servlet-mapping
<!-- uncomment this if you want the admin servlet --
<!--
<servlet-mapping
<servlet-nameAdminServlet</servlet-name
<url-pattern/servlet/AdminServlet</url-pattern
</servlet-mapping
--
<!-- currently the W3C havent settled on a media type for WSDL;
http://www.w3.org/TR/2003/WD-wsdl12-20030303/#ietf-draft
for now we go with the basic 'it's XML' response --
<mime-mapping
<extensionwsdl</extension
<mime-typetext/xml</mime-type
</mime-mapping
<mime-mapping
<extensionxsd</extension
<mime-typetext/xml</mime-type
</mime-mapping
</web-app
5.开启你的application server,把目录jagiews发布为一个名叫jagiews的web application。
6.测试:打开浏览器,输入网址(这里使用的是weblogic,其他的服务器请酌情修改):
http://localhost:7001/jagiews/services/StringProcess?method=process&name=qqqq,
如果浏览器能在返回的xml文档中显示字符串"QQQQ",恭喜你,你的web service发布成功了。如果发布不成功,请按以上发布步骤检查一下。
2. 客户端
客户端自然是用MIDlet了,不过用什么方式来访问web service呢?其实有3种访问方