j2me 调用 webservice

客户端

客户端自然是用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.*; public class WSClientMIDlet extends MIDlet{ static WSClientMIDlet instance; public WSClientMIDlet() { instance = this; } public void startApp() { Display display=Display.getDispla(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.2DisplayForm.java

package com.jagie.j2me.ws; import javax.microedition.lcdui.*; 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.3StringProcessorStub.java

package com.jagie.j2me.ws; import org.ksoap.*; import org.ksoap.transport.HttpTransport; public class StringProcessorStub { public StringProcessorStub() { } public String process(String name) { String result = null; try { SoapObject rpc = new SoapObje("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; } }

测试客户端

现在,试着在你的ide里运行WSClientMIDlet,如果调用成功,则出现以下画面:

test

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值