经过半个月的努力,终于把了解J2ME调用C# WebService(含参数)的方法,努力了好久,决定和大家分享。在分享之前,要感谢在这个过程中一直帮助我的ωǒs甲骨文和Je m'appelle,是他们的帮助让我掌握了这一方法。
以登陆为例进行分析
登陆代码:
- package com.sinic.logic;
- import javax.microedition.lcdui.Alert;
- import javax.microedition.lcdui.AlertType;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Form;
- import javax.microedition.lcdui.TextField;
- import com.sinic.main.FrmMain;
- /**
- * <p>Title: UserLogin</p>
- * <p>Description: 个性设置:登陆界面</p>
- * <p>Copyright: Copyright (c) 2008</p>
- * <p>Company: </p>
- * @author 朱久长
- * @version 1.0
- * */
- public class UserLogin extends Form implements CommandListener, Runnable{
- private TextField tfUser = new TextField("请编辑手机号码:", "", 11, TextField.NUMERIC);
- private TextField tfPass = new TextField("请输入服务密码:", "", 6, TextField.NUMERIC);
- private Display display = null;
- private FrmMain fm=null;
- private Displayable parent = null;
- private int result=0;
- private Thread t;
- private String user=null;
- private String pass=null;
- private UserMainForm userMain=null;
- private final static Command CMD_BACK = new Command("返回", Command.SCREEN, 1);
- private final static Command CMD_OK = new Command("确定", Command.SCREEN, 1);
- public UserLogin(FrmMain d, Displayable p) {
- super("个性设置--用户登陆");
- fm = d;
- this.parent = p;
- append(tfUser);
- append(tfPass);
- addCommand(CMD_OK);
- addCommand(CMD_BACK);
- setCommandListener(this);
- }
- public void commandAction(Command arg0, Displayable arg1) {
- String strUser = tfUser.getString().toString();
- if (arg0 == CMD_BACK) {
- fm.display.setCurrent(parent);
- } else if (arg0 == CMD_OK) {
- if(strUser.length()!=11){
- Alert a = new Alert("个性设置--用户登陆", "手机号码不正确", null, AlertType.INFO);
- display.setCurrent(a);
- }else{
- t=new Thread(this);
- t.start();
- }
- }
- }
- public void run(){
- user = tfUser.getString().toString();
- pass = tfPass.getString().toString();
- userMain=new UserMainForm(fm,this,user);
- result=new J2meToWS().checkUser(user, pass);
- if(result==1){
- fm.display.setCurrent(userMain);
- }else {
- Alert a = new Alert("个性设置--用户登陆", "登陆失败", null, AlertType.INFO);
- fm.display.setCurrent(a);
- System.out.println("~~~~登陆失败~~~~ ");
- }
- }
- }
J2meToWS类:包含checkUser(user, pass)方法
- package com.sinic.logic;
- import org.ksoap.SoapObject;
- import org.ksoap.transport.HttpTransport;
- //import org.ksoap2.SoapEnvelope;
- //import org.ksoap2.serialization.MarshalBase64;
- //import org.ksoap2.serialization.SoapSerializationEnvelope;
- /**
- * <p>Title: J2meToWS</p>
- * <p>Description: J2me To Web Service</p>
- * <p>Copyright: Copyright (c) 2008</p>
- * <p>Company: </p>
- * @author 朱久长
- * @version 1.0
- * */
- public class J2meToWS {
- public J2meToWS() {
- }
- public int checkUser(String tel,String pwd) {
- int result = 0;
- try {
- SoapObject rpc = new SoapObject("http://tempuri.org/", "Login");
- //tel、pwd是方法Login(string tel,string pwd)的两个参数,参数名和个数保持相同
- rpc.addProperty("tel", tel);
- rpc.addProperty("pwd", pwd);
- HttpTransport ht = new HttpTransport("http://localhost/SMSCALLWebService/AppService.asmx",
- "http://tempuri.org/Login");
- ht.debug=true;
- result =Integer.parseInt(ht.call(rpc).toString());
- }catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- }
所调用的WebService文件AppService.asmx含有Login(string tel,string pwd)方法。特别注意,必须加上[SoapRpcService(RoutingStyle = SoapServiceRoutingStyle.SoapAction)]这句声明方了。
- using System;
- using System.Web;
- using System.Collections;
- using System.Web.Services;
- using System.Web.Services.Protocols;
- using System.Data;
- using System.Text;
- /// <summary>
- /// 系统服务
- /// </summary>
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [SoapRpcService(RoutingStyle = SoapServiceRoutingStyle.SoapAction)]
- public class AppService : System.Web.Services.WebService
- {
- public AppService()
- {
- //如果使用设计的组件,请取消注释以下行
- //InitializeComponent();
- }
- [WebMethod(Description="系统登录 1:验证通过,2:退订用户,4:密码错误,8:无此用户")]
- public int Login(string tel,string pwd)
- {
- string sql = "SELECT STAT,PWD_NO FROM NO_INFO WHERE NO='" + tel + "'";
- DataTable dt = new DataAccess().GetDataTable(sql);
- if (dt != null && dt.Rows.Count == 1)
- {
- string passwordDB = dt.Rows[0]["PWD_NO"].ToString();
- if (string.Equals(pwd, passwordDB))
- {
- int statDB = Convert.ToInt32(dt.Rows[0]["STAT"]);
- return statDB == 1 ? 1 : 2;
- }
- else
- {
- return 4;
- }
- }
- else
- {
- return 8;
- }
- }
- }