在J2ME中访问dotnet Web Services
过JSR172规范来实现对Web Services的访问会是比较方便和快速的方式,可以访问其它任何工具创建的Web Services。本文将讨论使用该方式访问 Web 服务的实例。
有两种方式访问Web服务:1、通过JSR172 API。2、通过KSOAP API。通过JSR172规范来实现对Web Services的访问,可以访问其它任何工具创建的Web Services。
本示例开发环境:J2ME Wireless Toolkit 2.2,JB9,dotnet2003
需要的jar包:kxml-min.zip ,ksoap-midp.zip
步骤1:使用.NET 开发的Web 服务为:(确保调试通过)
[WebMethod(Description="Login"]
//[System.Web.Services.
Protocols.SoapRpcMethod]
public bool Login(string
sLoginUserID,string sLoginPwd)
{
string spwd="";
gUserID = "";
if((sLoginUserID == null)
|| (sLoginUserID.Trim() == ""))
{
return false;
}
try
{
myConnection = new SqlConnection(conStr);
string strSql = "SELECT
* FROM tUser WHERE userid=@UserID";
SqlCommand myCommand =
new SqlCommand(strSql, myConnection);
SqlParameter paramUserID =
new SqlParameter("@UserID", SqlDbType.NVarChar, 12);
paramUserID.Value = sLoginUserID;
myCommand.Parameters.Add(paramUserID);
myConnection.Open();
dataReader = myCommand.ExecuteReader
(CommandBehavior.CloseConnection);
while(dataReader!=null && dataReader.Read())
{
spwd = dataReader.GetString(2);
}
if( !spwd.Equals(sLoginPwd))
{
return false;
}
else
{
return true;
}
}
catch (Exception ex)
{
Error.Log(ex.Message.ToString());
return false;
}
finally
{
if(myConnection!=null)
myConnection.Close();
if(dataReader!=null)
dataReader.Close();
}
}
步骤2:在J2ME中引入Web服务。
在开始菜单中找到J2ME wireless Toolkit2.2中的Utilities一项,点击Stub Generator按钮,在弹出的界面上输入WSDL,例如:http://192.168.10.101/Service/MyServices.asmx?wsdl,注意一定要加wsdl.在outpath中填入你想将生成的访问Web服务的代码存放的目录;Output Package中填入你的工程src的目录,例如helloworld.WS是指src目录下的子目录helloworld下的目录WS--如果编译不通过,可以手工改。设定CLDC的版本1.0/1.1,建议用1.1的,支持浮点运算。
- 本文关键词:

