J2ME调用C# WebService的方法(含参数)
2008-10-29 14:39
531 查看
经过半个月的努力,终于把了解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;
}
}
}
以登陆为例进行分析
登陆代码:
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;
}
}
}
相关文章推荐
- Delphi 调用C#编写的WebService 参数为Null解决方法
- Delphi 调用C#编写的WebService 参数为Null解决方法
- Delphi 调用C#编写的WebService 参数为Null解决方法
- Delphi (c++builder) 调用C#编写的WebService 参数为Null解决方法
- Delphi 调用C#编写的WebService 参数为Null解决方法
- Delphi 调用C#编写的WebService 参数为Null解决方法
- Delphi 调用C#编写的WebService 参数为Null解决方法
- (转)Delphi 调用C#编写的WebService 参数为Null解决方法 附中文乱码问题
- Delphi 调用C#编写的WebService 参数为Null解决方法
- C# 通过Get、Post、Soap调用WebService的方法
- C#线程调用带参数的方法
- 调用带参数的webservice产生的问题和解决的方法
- C# 调用C++DLL传递指向指针的指针参数的方法
- PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- C#线程调用带参数的方法
- C#线程调用带参数的方法 - 转
- c#调用存储过程时 SqlParameter 中传递参数时ref参数的设置方法
- JQuery的Ajax使用Get,Post方法调用C#WebService并返回数据
- .Net下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#)