- 浏览: 22051417 次
- 性别:
- 来自: 杭州
最新评论
-
ZY199266:
配置文件还需要额外的配置ma
Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程 -
ZY199266:
我的一访问为什么是 /mavenwebdemo/WEB-I ...
Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程 -
lvgaga:
我又一个问题就是 如果像你的这种形式写。配置文件还需要额外的 ...
Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程 -
lvgaga:
我的一访问为什么是 /mavenwebdemo/WEB-I ...
Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程 -
y1210251848:
你的那个错误应该是项目所使用的目标框架不支持吧
log4net配置(web中使用log4net,把web.config放在单独的文件中)
[J2ME/kSOAP]kSOAP的运用讲义[去年8月手稿]
去年8月份的一份手稿,完整描述了我对j2me-kSOAP如何和服务器端的Web Service交互的经验和教训。本手稿已刊登在mingjava兄弟的新书中。
[j2me]kSOAP的运用
编写者 | 日期 | 关键词 |
郑昀@ultrapower | 2006-8-24 | J2me webservice ksoap |
1.概述
对于J2ME访问远端的Web Service,除了官方标准JSR 172,我们还有两种选择:
l kSOAP
l Wingfoot
Wingfoot是由Wingfoot Software(www.wingfoot.com)出品的一款J2ME(CLDC/CDC) SOAP1.1的轻量级实现方案。
kSOAP是Enhydra.org的一个开源作品,是EnhydraME项目的一部分。基于Enhydra.org出品的开源通用XML解析器kXML,kSOAP完成了J2ME/MIDP平台上的SOAP解析和调用工作。
Stefan Haustein领导的kSOAP开发小组于2001年5月17日推出了Alhpa版本。之后又经过了一年的开发,2002年6月6日推出的kSOAP 1.2支持了SOAP1.2规范。2003年8月25日推出的kSOAP2,对SOAP序列化规范支持得更好了。
大多数人选择kSOAP的原因是,kSOAP虽然在2003年8月之后就不再维护了,但它是Open Source的,很容易加入增强特性,比如说默认情况下kSOAP2仅仅支持cmnet接入点,可以修改kSOAP2的HttpTransport.java代码增加对cmwap接入点的支持。
<group id="_x0000_s1032" style="WIDTH: 6in; HEIGHT: 93.6pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" coordsize="6646,1444" coordorigin="2754,7135" editas="canvas"><lock aspectratio="t" v:ext="edit"></lock><shapetype id="_x0000_t75" coordsize="21600,21600" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_s1033" style="LEFT: 2754px; WIDTH: 6646px; POSITION: absolute; TOP: 7135px; HEIGHT: 1444px" o:preferrelative="f" type="#_x0000_t75"><font size="3"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></font></shape><roundrect id="_x0000_s1034" style="LEFT: 2754px; WIDTH: 6508px; POSITION: absolute; TOP: 7226px; HEIGHT: 1233px" fillcolor="#ffc" arcsize="10923f"><fill type="gradient" focus="100%" rotate="t"></fill><textbox style="mso-next-textbox: #_x0000_s1034"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><shape id="_x0000_i1032" style="WIDTH: 15.75pt; HEIGHT: 22.5pt" type="#_x0000_t75"><imagedata o:title="light bulb" src="file:///C:/DOCUME~1/zhengyun/LOCALS~1/Temp/msohtml1/12/clip_image001.png"></imagedata></shape></span><font face="Times New Roman"><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 14pt; COLOR: navy; FONT-FAMILY: 华文楷体">下载提示</span></strong><span style="FONT-FAMILY: 华文楷体"><font size="3">:</font></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kSOAP</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当前有两个版本:</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">1.2</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">2.0</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">官方网站:</span><u><span lang="EN-US" style="COLOR: blue; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"><a href="http://ksoap.objectweb.org/"><font color="#0000cc">http://ksoap.objectweb.org/</font></a><p></p></span></u></font></p> </div> </td> </tr></tbody></table></textbox></roundrect><wrap type="none"></wrap><anchorlock></anchorlock></group>
kSOAP2.0还有一个优点是,改进了对Microsoft dotNET的兼容。以前有很多人抱怨kSOAP调用dotNET编写的Web Service时遇到了不少的困扰。
本章节我们将使用kSOAP 2.0的例子来讲解。
为了使用kSOAP 2.0,必须还要下载工具包kXML2。
<roundrect id="_x0000_s1031" style="WIDTH: 423.05pt; HEIGHT: 79.9pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" fillcolor="#ffc" arcsize="10923f"><fill type="gradient" focus="100%" rotate="t"></fill><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><shape id="_x0000_i1033" style="WIDTH: 15.75pt; HEIGHT: 22.5pt" type="#_x0000_t75"><imagedata o:title="light bulb" src="file:///C:/DOCUME~1/zhengyun/LOCALS~1/Temp/msohtml1/12/clip_image001.png"></imagedata></shape></span><font face="Times New Roman"><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 14pt; COLOR: navy; FONT-FAMILY: 华文楷体">下载提示</span></strong><span style="FONT-FAMILY: 华文楷体"><font size="3">:</font></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kXML</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当前有两个版本:</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">1.21</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">2.0</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">官方网站:</span><span lang="EN-US" style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt"><a href="http://kxml.objectweb.org/"><font color="#0000cc">http://kxml.objectweb.org/</font></a> </span><u><span lang="EN-US" style="COLOR: blue; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"><p></p></span></u></font></p> </div> </td> </tr></tbody></table></textbox><wrap type="none"></wrap><anchorlock></anchorlock></roundrect>
kXML2比kXML更小更快。
2.kSOAP2接口
让我们先熟悉一下即将用到的kSOAP2的常用接口。
接口 |
org.ksoap2. SoapEnvelope org.ksoap2. SoapSerializationEnvelope org.ksoap2. SoapObject org.ksoap2.transport. HttpTransport |
SoapEnvelope对应于SOAP规范中的SOAP Envelope,封装了head和body对象。
SoapSerializationEnvelope是kSOAP2新增加的类,是对SoapEnvelope的扩展,对SOAP序列化(Serialization)格式规范提供了支持,能够对简单对象自动进行序列化(simple object serialization)。而kSOAP1.x则是通过org.ksoap.ClassMap来做序列化的,不太好操作,也不利于扩展。
SoapObject让你自如地构造SOAP调用;
HttpTransport为你屏蔽了Internet访问/请求和获取服务器SOAP的细节。
下面我们通过一个最简单的webservice调用,来看看kSOAP是如何做到SOAP解析的:
2.1.kSOAP和Web Service之间传递String
webservice传递String给MIDP是一件很简单的事情。首先在服务器端,不管你是用Microsft ASP.NET创建webservice,还是由Tomcat+AXIS1.2支撑的webservice,都可以这么编写主服务类:
服务器端 |
public class SimpleKSoapWS {
public SimpleKSoapWS () { }
public String foo(String username, String password) { return “fooResult”; } } |
kSOAP是如何调用这个webservice的呢?
首先要使用SoapObject,这是一个高度抽象化的类,完成SOAP调用。可以调用它的addProperty方法填写要调用的webservice方法的参数。如下面代码所示:
SoapObject request = new SoapObject(serviceNamespace, methodName);
SoapObject构造函数的两个参数含义为:
serviceNamespace – 你的webservice的命名空间,既可以是
http://localhost:8088/flickrBuddy/services/Buddycast这样的,也可以是
urn:PI/DevCentral/SoapService这样的;
methodName – 你要调用方法的名字。
然后,按照webservice方法参数的顺序,依次调用
request.addProperty( "username", "user" );
request.addProperty( "password", "pass" );
来填充webservice参数。
<roundrect id="_x0000_s1027" style="WIDTH: 423.05pt; HEIGHT: 82.05pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" fillcolor="#ffc" arcsize="10923f"><fill type="gradient" focus="100%" rotate="t"></fill><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><shape id="_x0000_i1034" style="WIDTH: 15.75pt; HEIGHT: 22.5pt" type="#_x0000_t75"><imagedata o:title="light bulb" src="file:///C:/DOCUME~1/zhengyun/LOCALS~1/Temp/msohtml1/12/clip_image001.png"></imagedata></shape></span><font face="Times New Roman"><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 14pt; COLOR: navy; FONT-FAMILY: 华文楷体">注意</span></strong><span style="FONT-FAMILY: 华文楷体"><font size="3">:</font></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">建议</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">webservice</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的方法传递的参数尽量用</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">string</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类型。即使是</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">int</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类型,</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kSOAP2</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">与</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">Java</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">编写的</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">webservice</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">也有可能交互发生异常。</span><u><span lang="EN-US" style="COLOR: blue; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"><p></p></span></u></font></p> </div> </td> </tr></tbody></table></textbox><wrap type="none"></wrap><anchorlock></anchorlock></roundrect>
对于webservice方法返回String类型的情况,还用不着开发者做序列化(Serialization)定制工作。
<roundrect id="_x0000_s1030" style="WIDTH: 423.05pt; HEIGHT: 160.35pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" fillcolor="#ffc" arcsize="10923f"><fill type="gradient" focus="100%" rotate="t"></fill><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><shape id="_x0000_i1035" style="WIDTH: 15.75pt; HEIGHT: 22.5pt" type="#_x0000_t75"><imagedata o:title="light bulb" src="file:///C:/DOCUME~1/zhengyun/LOCALS~1/Temp/msohtml1/12/clip_image001.png"></imagedata></shape></span><font face="Times New Roman"><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 14pt; COLOR: navy; FONT-FAMILY: 华文楷体">要点</span></strong><span style="FONT-FAMILY: 华文楷体"><font size="3">:</font></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kSOAP 1.X/2.0</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">可以自动把四种<span lang="EN-US">SOAP</span>类型映射为<span lang="EN-US">Java</span>类型<span lang="EN-US"><p></p></span></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt"><font size="3">SOAP type<span style="mso-tab-count: 1"> </span><span style="mso-tab-count: 2"> </span>Java type<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt"><font size="3">xsd:int <span style="mso-tab-count: 1"> </span><span style="mso-tab-count: 2"> </span>java.lang.Integer<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt"><font size="3">xsd:long <span style="mso-tab-count: 1"> </span><span style="mso-tab-count: 2"> </span>java.lang.Long<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt"><font size="3">xsd:string <span style="mso-tab-count: 1"></span><span style="mso-tab-count: 2"> </span>java.lang.String <p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><span lang="EN-US" style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt"><font size="3">xsd:boolean<span style="mso-tab-count: 1"> </span><span style="mso-tab-count: 2"> </span>java.lang.Boolean<p></p></font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt">除此之外,都需要开发者自己做类型映射。</span><u><span lang="EN-US" style="COLOR: blue; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"><p></p></span></u></font></p> </div> </td> </tr></tbody></table></textbox><wrap type="none"></wrap><anchorlock></anchorlock></roundrect>
然后要告诉SoapSerializationEnvelope把构造好的SoapObject封装进去:
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
<roundrect id="_x0000_s1029" style="WIDTH: 423.05pt; HEIGHT: 147.45pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" fillcolor="#ffc" arcsize="10923f"><fill type="gradient" focus="100%" rotate="t"></fill><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><shape id="_x0000_i1036" style="WIDTH: 15.75pt; HEIGHT: 22.5pt" type="#_x0000_t75"><imagedata o:title="light bulb" src="file:///C:/DOCUME~1/zhengyun/LOCALS~1/Temp/msohtml1/12/clip_image001.png"></imagedata></shape></span><font face="Times New Roman"><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 14pt; COLOR: navy; FONT-FAMILY: 华文楷体">要点</span></strong><span style="FONT-FAMILY: 华文楷体"><font size="3">:</font></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">你可以通过</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SoapSerializationEnvelope</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">或者</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SoapEnvelope</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的构造函数来指明你要用</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SOAP</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的哪一个规范,可以是以下几种之一:</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">常量</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SoapEnvelope.VER10</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:对应于</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SOAP 1.0</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">规范</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">常量</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SoapEnvelope.VER11</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:对应于</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SOAP 1.1</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">规范</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">常量</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SoapEnvelope.VER12</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">:对应于</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SOAP 1.2</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">规范</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这样,无论要调用的</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">webservice</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">采用了哪一个</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SOAP</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">规范,你都可以轻松应对。</span><u><span lang="EN-US" style="COLOR: blue; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-font-kerning: 0pt; mso-bidi-font-family: 宋体"><p></p></span></u></font></p> </div> </td> </tr></tbody></table></textbox><wrap type="none"></wrap><anchorlock></anchorlock></roundrect>
接下来就要声明
HttpTransport tx = new HttpTransport(serviceURL);
ht.debug = true;
HttpTransport构造函数的参数含义为:
serviceURL – 要投递SOAP数据的目标地址,譬如说
http://soap.amazon.com/onca/soap3 。
HttpTransport是一个强大的辅助类,来完成Http-call transport process,它封装了网络请求的一切,你完全不用考虑序列化消息。我们通过设置它的debug属性为true来打开调试信息。
方法HttpTransport.call()自己就能够发送请求给服务器、接收服务器响应并序列化SOAP消息,如下所示:
ht.call(null, envelope);
HttpTransport的call方法的两个参数含义为:
soapAction – SOAP 规范定义了一个名为 SOAPAction 的新 HTTP 标头,所有 SOAP HTTP 请求(即使是空的)都必须包含该标头。 SOAPAction 标头旨在表明该消息的意图。通常可以置此参数为null,这样HttpTransport就会设置HTTP标头SOAPAction为空字符串。
Envelope – 就是前面我们构造好的SoapSerializationEnvelope或SoapEnvelope对象。
<roundrect id="_x0000_s1028" style="WIDTH: 423.05pt; HEIGHT: 142.8pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" fillcolor="#ffc" arcsize="10923f"><fill type="gradient" focus="100%" rotate="t"></fill><textbox style="mso-next-textbox: #_x0000_s1028"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><shape id="_x0000_i1037" style="WIDTH: 15.75pt; HEIGHT: 22.5pt" type="#_x0000_t75"><imagedata o:title="light bulb" src="file:///C:/DOCUME~1/zhengyun/LOCALS~1/Temp/msohtml1/12/clip_image001.png"></imagedata></shape></span><font face="Times New Roman"><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 14pt; COLOR: navy; FONT-FAMILY: 华文楷体">注意</span></strong><span style="FONT-FAMILY: 华文楷体"><font size="3">:</font></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">HttpTransport</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的处理上,</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kSOAP2</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kSOAP1.2</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的写法不一样。</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kSOAP 1.2</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">HttpTransport</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的构造函数是</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">HttpTransport (String url, String soapAction)</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,第二个参数</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">soapAction</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可以是要调用的</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">webservice</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法名。</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">而</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kSOAP 2</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">,构造函数是</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman"> HttpTransport(String url)</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kSOAP2</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">相当于把</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">webservice</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法名分离出去,完全交给</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SoapObject</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">去封装,而</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">HttpTransport</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">仅仅负责把</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">SoapEnvelope</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">发送出去并接收响应,这样更合理一些。</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> </div> </td> </tr></tbody></table></textbox><wrap type="none"></wrap><anchorlock></anchorlock></roundrect>
调用call方法是一个同步过程,需要等待它返回。
返回之后,就可以调用SoapSerializationEnvelope的getResult方法来获取结果了:
Object Response = envelope.getResult();
如果HttpTransport的debug属性为true,那么此时就可以通过
System.out.println("Response dump>>" + tx.responseDump);
打印出HttpTransport的调试信息。尤其当前面call方法和getResult方法发生异常时,这个调试信息是非常有用的。
前面我们的webservice方法由于是返回string,所以得到这个string值就非常简单了:
String sResponse = (String)Response;
<roundrect id="_x0000_s1026" style="WIDTH: 423.05pt; HEIGHT: 75.45pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" fillcolor="#ffc" arcsize="10923f"><fill type="gradient" focus="100%" rotate="t"></fill><textbox style="mso-next-textbox: #_x0000_s1026"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr> <td style="BORDER-RIGHT: #ece9d8; BORDER-TOP: #ece9d8; BORDER-LEFT: #ece9d8; BORDER-BOTTOM: #ece9d8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><shape id="_x0000_i1038" style="WIDTH: 15.75pt; HEIGHT: 22.5pt" type="#_x0000_t75"><imagedata o:title="light bulb" src="file:///C:/DOCUME~1/zhengyun/LOCALS~1/Temp/msohtml1/12/clip_image001.png"></imagedata></shape></span><font face="Times New Roman"><strong style="mso-bidi-font-weight: normal"><span style="FONT-SIZE: 14pt; COLOR: navy; FONT-FAMILY: 华文楷体">注意</span></strong><span style="FONT-FAMILY: 华文楷体"><font size="3">:</font></span></font></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><font size="3"><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">由于</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">HttpTransport</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类实际上是调用了</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">HttpConnection</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">作网络连接,所以必须另起一个线程来专门做</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><font face="Times New Roman">kSOAP</font></span><span style="COLOR: navy; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">工作,否则会堵塞操作。</span><span lang="EN-US" style="COLOR: navy; mso-bidi-font-size: 10.5pt"><p></p></span></font></p> </div> </td> </tr></tbody></table></textbox><wrap type="none"></wrap><anchorlock></anchorlock></roundrect>
综上所述,J2ME客户端的MIDlet按键事件函数这么写即可:
MIDlet codes |
import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransport;
public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } if (c == connectCommand) { // 匿名内部Thread,调用kSOAP2访问远程服务。 Thread webserviceThread = new Thread() { public void run(){ try { String serviceNamespace = "http://localhost:8080/SimpleWS/services/SimpleKSoapWS"; String methodName = "foo"; String serviceURL = "http://localhost:8080/SimpleWS/services/SimpleKSoapWS";
SoapObject request = new SoapObject(serviceNamespace, methodName); request.addProperty( "username", "user" ); request.addProperty( "password", "pass" );
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = request;
HttpTransport tx = new HttpTransport(serviceURL); ht.debug = true; ht.call(null, envelope); Object Response = envelope.getResult(); /* * 必要时打印出tx.responseDump来观察soap是否正确工作 */ System.out.println("dump>>" + tx.responseDump); String sResponse = (String)Response; } catch (Exception e) { e.printStackTrace (); } } }; webserviceThread.start(); } |
2.2.webservice返回复杂描述的情况
kSOAP2处理webservice简单的string类型返回值是很容易的。那么如何处理像亚马逊网上书店这种webservice返回的复杂描述呢?
kSOAP2自带了一个例子来说明,下面我们就讲解一下。
关于亚马逊的查询书目的webservice,你可以通过
http://soap.amazon.com/schemas3/AmazonWebServices.wsdl
来获知定义。
我们要关注的是它的关键词查询请求的方法,它的定义是:
operation name="KeywordSearchRequest">
soap:operation soapAction="http://soap.amazon.com" />
soap:body use="encoded"
encodingStyle=http://schemas.xmlsoap.org/soap/encoding/ namespace="http://soap.amazon.com" />
input>
output>
soap:body use="encoded"
encodingStyle=http://schemas.xmlsoap.org/soap/encoding/ namespace="http://soap.amazon.com" />
output>
operation>
我们提交对包含指定关键词的书目查询,如果查询成功,将会返回一系列书名节点,每一本书都提供了作者、出版社、出版日期、价格等等信息。这些书名节点都在一个“Details”节点下。查询结果的总数放在TotalResults节点。每页10个结果,可以通过查看TotalPages节点来确定需要多少页。
那么,kSOAP2可以很简单地通过SoapObject的getProperty方法来得到书详细信息的节点,存储入一个Vector对象中,如下所示:
HttpTransport ht = new HttpTransport("http://soap.amazon.com/onca/soap3");
ht.call(null, envelope);
SoapObject result = (SoapObject) envelope.getResult();
Vector resultVector = (Vector) result.getProperty("Details");
Vector对象中实际上还是存储了一组SoapObject对象,这里的每一个SoapObject对象对应于一本书的DOM对象。
那么如何得到每一本书的书名、价格呢?
for(int i = 0; i
SoapObject detail = (SoapObject) resultVector.elementAt(i);
System.out.println("书名>>"+(String) detail.getProperty("ProductName"));
System.out.println("日期>>"+(String) detail.getProperty("ReleaseDate"));
System.out.println("价格>>"+(String) detail.getProperty("ListPrice"));
}
这样就可以了。
需要注意的是,要测试这个工程,必须到亚马逊的http://www.amazon.com/webservice 注册获取Access Key ID,也就是webservice方法中的“devtag”参数所需要的Developer-Tag。
2.3.webservice传递自定义复杂对象
下面我们讲述如何在MIDP设备和webservice之间传递自定义类,比如这个类中不但有String类型成员变量,还有Vector之类的复杂类型。
大致思路就是,在服务器端将类实例按照一定规格(一个一个的成员变量写)序列化为byte[],将这个byte[]数组返回给kSOAP2。kSOAP2收到之后,再反序列化,将byte[]一段一段地读入类实例。
2.3.1.webservice服务器端的做法
我们先来定义要传递的wsTeam类:
类定义 |
public class wsTeam{ private String wsReturnCode; private String wsPersonCount; public StringVector wsvPersonName; public byte[] serialize(); public static wsTeam deserialize(byte[] data) ; } |
其中,StringVector类是另外一个自定义类,就是简单地把String[]封装了一下,便于操作。StringVector类定义在示范代码中可以找到。
服务器端主要是序列化,所以我们来讲讲wsTeam的serialize()函数。
wsTeam的序列化函数 |
public byte[] serialize() { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos);
try { dos.writeUTF(wsReturnCode); dos.writeUTF(wsPersonCount); wsvPersonName.writeObject(dos); baos.close(); dos.close(); } catch(Exception exc) { exc.printStackTrace(); }
return baos.toByteArray(); } |
这样,类实例就可以把自己序列化为byte[]数组。
那么,webservice可以这么提供:
服务器端 |
public class SimpleKSoapWS {
public SimpleKSoapWS () { }
public byte[] foo2(String username, String password) { wsTeam obj= new wsTeam (); return obj.serialize(); } } |
到了MIDP设备上,要能够从byte[]恢复出wsTeam类实例才行。
StringVector的序列化方法writeObject也很简单,先写入字符串数组的大小,然后再将每一个元素写入,如下所示:
StringVector的序列化 |
public class StringVector {… public synchronized void writeObject(java.io.DataOutputStream s) throws java.io.IOException { // Write out array length s.writeInt(count); // Write out all elements in the proper order. for (int i=0; i<count i></count> { s.writeUTF(data[i]); } } … } |
2.3.2.MIDP设备的做法
和前面的MIDlet代码差不多,只不过要kSOAP2的MarshalBase64出场了。
在kSOAP中,我们用Base64把二进制流编码为ASCII字符串,这样就可以通过XML/SOAP传输二进制数据了。
org.ksoap2.serialization.MarshalBase64的目的就是,把SOAP XML中的xsd:based64Binary元素序列化为Java字节数组(byete array)类型。类似的,kSOAP2还提供了MarshalDate、MarshalHashtable类来把相应的元素序列化为Java的Date、Hashtable类型。
使用MarshalBase64 |
import org.ksoap2.serialization.MarshalBase64;
SoapObject request = new SoapObject(serviceNamespace, methodName );
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = request; new MarshalBase64().register(envelope);
HttpTransport tx = new HttpTransport(serviceNamespace); tx.debug = true;
tx.call(null, envelope); Object Response = envelope.getResult(); |
将接收到的SoapObject强制转换为byte[]。
转换 |
byte[] by = (byte[])Response; System.out.println("succ convert!"); |
然后,再调用
反序列化 |
wsTeam wc = wsTeam.deserialize(by); |
这样,在无线设备上就得到了wsTeam类实例了。
wsTeam的deserialize函数是这么定义的:
wsTeam的反序列化函数 |
public class StringVector {… public static wsTeam deserialize(byte[] data) { ByteArrayInputStream bais = new ByteArrayInputStream(data); DataInputStream dis = new DataInputStream(bais); wsTeam wc = new wsTeam();
try { wc.wsReturnCode = dis.readUTF(); wc.wsPersonCount = dis.readUTF();
wc. wsvPersonName.readObject(dis);
bais.close(); dis.close(); } catch(Exception exc)
exc.printStackTrace(); }
return wc; } …} |
StringVector的反序列化方法readObject也很简单,先读入字符串数组的大小,就自行新建一个同样大小的字符串数组,然后再将每一个元素写入这个数组,如下所示:
StringVector的反序列化 |
public class StringVector {… public synchronized void readObject(java.io.DataInputStream s) throws java.io.IOException, ClassNotFoundException { // Read in array length and allocate array int arrayLength = s.readInt(); data = new String[arrayLength]; // 同步data的大小 count = arrayLength; // Read in all elements in the proper order. for (int i=0; i<arraylength i></arraylength> { data[i] = s.readUTF(); } }… } |
通过上面的反序列化,我们就可以通过
for (int i=0; i<wc.wsvpersonname.size i></wc.wsvpersonname.size>
System.out.println("第" + i +"个人:" +
wc.wsvPersonName.getStringAt(i));
}
来打印MIDlet上收到的类对象中的StringVector成员变量了。
3.小结
利用kSOAP2提供的框架,你可以在无线设备和Internet webservice之间,既可以传递简单的数值,也可以传递各种各样的类对象。
相关推荐
java代理/j2ee/j2me/ie
J2ME/J2EE结合使用经典例程
“J2ME使用KSoap调用C#开发的Web Service”这个标题表明,本主题涉及的是在Java 2 Micro Edition (J2ME)平台上,如何利用KSoap库来调用由C#语言编写的Web服务。J2ME是Java的一种轻量级实现,用于移动设备和嵌入式...
基于J2ME/Mobile SVG的移动地理信息服务客户端技术研究,范财友,冯立建,本文在分析当前移动地理信息服务应用与技术的基础上,详细讨论了基于J2ME/Mobile SVG技术实现移动地理信息服务的基本原理,系统框架和
随着手机的日益普及和Java功能在移动设备上的实现,Java...该系统的设计采用了面向对象的方法,对J2ME客户端体系架构,J2EE 服务器体系结构以及移动客户端与J2EE服务器集成进行了设计,实现了现有的Web校友录的基本功能
kSOAP是Enhydra.org的一个开源作品,是EnhydraME项目的一部分。基于Enhydra.org出品的开源通用XML解析器kXML,kSOAP完成了J2ME/MIDP平台上的SOAP解析和调用工作。
有个朋友刚学j2me 问简单点的进度条怎么实现 就把这个发给他了 现在打包发出来给需要的初学者看 用途举例: 进入游戏界面之前先显示一下进度条 在loading的同时把需要的资源载入 初始化等等 eclipse写的 直接导入...
给诺基亚塞班用的新浏览器
**J2ME移动应用开发讲义** Java 2 Micro Edition(J2ME)是Java平台的一个子集,专为资源有限的嵌入式设备,如移动电话、PDA和家用电器设计。它允许开发者创建在这些设备上运行的应用程序,提供了一种跨平台的解决...
**J2ME移动开发实战教学视频讲义** J2ME,全称为Java 2 Micro Edition,是Java平台的一个子集,专为资源有限的嵌入式设备和移动设备设计。这个技术允许开发者创建可以在手机、PDA、智能电视等设备上运行的应用程序...
**KSOAP-J2ME-MIDP-JAVADOC** KSOAP是Java开发中的一个关键库,专门用于在J2ME(Java 2 Micro Edition)和MIDP(Mobile Information Device Profile)平台上实现SOAP(Simple Object Access Protocol)通信。SOAP是...
《郭克华J2ME移动开发实战教学视频讲义(全部)》是一份全面深入讲解J2ME(Java 2 Micro Edition)移动开发的珍贵资源,由知名讲师郭克华倾力打造。J2ME是Java平台的一个子集,专为资源有限的设备如手机、嵌入式系统等...
8. **安全性考虑**:在使用 kSOAP2 进行通信时,安全问题不容忽视。这可能包括加密传输(HTTPS)、认证机制以及对敏感数据的保护。 9. **示例应用**:压缩包中的源码可能是作者开发的一个示例应用,展示了如何使用 ...
**J2ME API/中文开发文档** Java 2 Micro Edition(J2ME)是Java平台的一个子集,专为资源有限的嵌入式设备和移动设备设计,如手机、智能手表和家用电器等。J2ME API是开发这些设备应用程序的基础,提供了丰富的...
Web服务和kSOAP的运用详解 在Java ME(J2ME)环境中,访问远程Web服务有多种方式。本文主要关注的是kSOAP库的使用,它是一个轻量级的解决方案,适用于J2ME平台上的SOAP(Simple Object Access Protocol)通信。...
kSOAP2-j2ME是kSOAP2的轻量级版本,专门设计用于资源有限的J2ME环境。这个库提供了一个简单的API,开发者可以使用它来构建SOAP请求,发送到Web服务,并解析返回的响应。kSOAP2-j2ME支持SOAP 1.1规范,包括基本的HTTP...
但J2ME对此提供的支持却十分有限,如MIDP2.0仅支持图像成90度整数倍的旋转。针对这种情况,本文提出了一种实现图像任意角度旋转的方案并提供了算法实现。然后对算法的应用问题进行了探讨,分析了算法的局限性。最后...
Java 2 Micro Edition (J2ME) 是一种Java平台,专为嵌入式设备和移动设备,特别是手机设计。在2000年代初期,J2ME是许多非智能手机上实现互联网浏览的主要技术,其中“j2me手机浏览器”就是这样一个应用。这个应用...