快捷搜索:

J2ME与Web Service-KSOAP的罗曼史

J2ME与Web Service-KSOAP的罗曼史

----J2ME与Web Service-KSOAP的快速上手

作者:Cleverpig

版权声明:可以随意率性转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本声明

原文地址:

http://www.matrix.org.cn/resource/article/43/43662_J2ME_KSOAP.html

关键词: j2me soap webservice

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置于: jagiewsWEB-INFclassescomjagiej2mews目录下。

3.在jagiewsWEB-INFlib 文件夹中置入以下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.在jagiewsWEB-INF目录下增添2个宣布描述文件:

server-config.wsdd,web.xml。

#server-config.wsdd

"adminPassword" value="admin"/>

"attachments.Directory"

value="C:Program Files

Apache Tomcat 4.0webapps

axisWEB-INFattachments"/>

"attachments.implementation"

value="org.apache.axis.

attachments.AttachmentsImpl"/>

"sendXsiTypes"

value="true"/>

"sendMultiRefs"

value="true"/>

"sendXMLDeclaration"

value="true"/>

"axis.sendMinimizedElements"

value="true"/>

name="scope"

value="session"/>

name="scope"

value="request"/>

name="extension"

value=".jwr"/>

value="AdminService"/>

value="false"/>

value="org.apache.axis.utils.Admin"/>

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

value="getVersion"/>

value="org.apache.axis.Version"/>

value="process"/>

value="com.jagie.j2me.

ws.StringProcessor"/>

# web.xml

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

AdminServlet

/servlet/AdminServlet

-->

wsdl

text/xml

xsd

text/xml

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种造访要领

直接用HttpConnection造访 http://localhost:7001/jagiews/services/StringProcess?method=process&name=qqqq,获得xml的返回数据,然后用kxml(http://kxml.enhydra.org/)解析,获得返回值。

假如你的手机支持MIDP2.0的话,可以斟酌应用JSR172。

用ksoap api。

这里讲述第三种要领。应用之前,你必要从 http://ksoap.enhydra.org/software/downloads/index.html下载稳定的ksoap包,置于你的classpath中。

2.1 客户端源代码

2.1.1 WSClientMIDlet.java

package com.jagie.j2me.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.notifyDestroyed();

instance = null;

}

}

2.1.2 DisplayForm.java

package com.jagie.j2me.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("字符转换webservice测试");

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;

/**

*

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/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;

}

}

总结

有了ksoap,手机上调用web service就很轻易了。不过要留意的是,应用收集连接这种费时操作的时刻,必然要零丁开线程进行,不要直接写在commandAction()措施里,否则呈现画面被锁住的环境。

您可能还会对下面的文章感兴趣: