webservice -------(4)-----------ksoap 2 对象作为参数
注:代码质量不高,只是demo,请谅解
1、服务代码:
UserObjectService.java
package com.user.service;import com.user.bean.User;public class UserObjectService {public UserObjectService(){}/** * * 比如用于用户登录 * * @param sql 客户端发到服务器的sql查询条件 * * @return 用户对象 */public User getUser(String sql){User user = new User();if(sql.equals("hello")){//不做与数据库之间的操作user.setUsername("hua");user.setPassword("123");} return user;}}
package com.user.bean;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;/** * * 用户的JavaBean * * @author Administrator * */public class User {String username;//用户名String password;//密码public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
package com.user.client;import java.io.IOException;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import org.ksoap2.*;import org.ksoap2.serialization.*;import org.ksoap2.transport.*;import org.xmlpull.v1.XmlPullParserException;public class TestUser2 extends MIDlet implements CommandListener {private Display dis;private Command connectCmd = new Command("连接", Command.OK, 1);private Command exitCmd = new Command("退出", Command.EXIT, 1);private Form f;private StringItem si = new StringItem("", "");public TestUser2() {}protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}protected void pauseApp() {}protected void startApp() throws MIDletStateChangeException {dis = Display.getDisplay(this);f = new Form("调用Web Service");f.append("要显示的内容:" + "\n");f.addCommand(connectCmd);f.addCommand(exitCmd);f.setCommandListener(this);dis.setCurrent(f);}public void commandAction(Command c, Displayable s) {if (c == exitCmd){try {destroyApp(false);notifyDestroyed();} catch (MIDletStateChangeException e) {e.printStackTrace();}}if (c == connectCmd) {//匿名内部Thread,调用ksoap2访问远程服务 Thread webserviceThread = new Thread() {public void run() {String serviceNamespace ="http://service.user.com";String methodName = "getUser";String serviceURL = "http://localhost:8090/WebServiceProject/services/UserObjectService";SoapObject request = new SoapObject(serviceNamespace,methodName);request.addProperty("sql", "hello");SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);envelope.bodyOut = request;HttpTransport ht = new HttpTransport(serviceURL);ht.debug = true;String Response = null;try {ht.call(null, envelope);//和.NET中SoapRpcMethod的Action一样 } catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (XmlPullParserException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}SoapObject ret = new SoapObject(serviceNamespace,"User");try {ret =(SoapObject)envelope.getResponse();} catch (SoapFault e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("*******"+ret.getProperty("username").toString());System.out.println("*******"+ret.getProperty("password").toString());System.out.println("dump>>" + ht.responseDump);si.setText(Response);f.append(si);f.removeCommand(connectCmd);}};webserviceThread.start();}}}