WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程教您在J2ME中访问dotnet Web Services

教您在J2ME中访问dotnet Web Services
作者:fosux  来源:转载  发布时间:2005-7-29 8:57:44
经过大量的试验证明,目前最方便、快速的方式就是通过JSR172规范来实现对Web Services的访问,可以访问其它任何工具创建的Web Services。 

目前,有两种方式访问Web服务:1、通过JSR172 API。 

2、通过KSOAP API。 

由于使用KSOAP方式访问Web服务的例子很多,尤其是访问使用Java开发的Web服务,但是使用KSOAP方式访问用.NET 开发的Web服务,目前的例子很少,而且我花了几天时间没有调通一个,原因未明,故只讨论使用JSR172的方式。KSOAP的方式这里暂不讨论。 

注:要获知KSOAP的详细资料请上:http://ksoap.objectweb.org/ 
要获知JSR172的资料请上:http://java.sun.com/products/wsa/ 

本示例开发环境: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的,支持浮点运算。  

点击OK按钮,就可以产生访问Web服务的代码了。将代码copy或者本身就产生在自己的工程目录中,刷新JB9的开发环境,新产生的代码即可出现。保证编译通过。  

步骤3:使用Web服务。  

修改你的MIDLet:  

例如:  


/** Service connector jax-rpc  
 stub for connecting to server. */ 
  private SalesServiceSoap_Stub service; 
  //这里写你自己的服务, 
  产生的java文件中有一个XXSoap_Stub.java文件, 
  其中XX就是你的Web服务名。 
  ...... 
  /** Initialize midlet data, service, parsers */ 
  public void startApp() 
  { 
    service = new SalesServiceSoap_Stub(); 
//new一个实例 
 service._setProperty 
 (SalesServiceSoap_Stub.SESSION_MAINTAIN_PROPERTY, 
new Boolean(true)); 
  ...... 




注意下面这段代码就使用Web服务的Login方法了。  


public void commandAction(Command c, Item item) 

    if (c == CMD_LOGIN) 
 { 
      sLoginUserID = txtUserID.getString(); 
      sLoginPwd = txtUserPwd.getString(); 
      if (sLoginUserID.length() == 0) 
   { 
        error("Input userid please!"); 
        return; 
      } 
      if (sLoginPwd.length() == 0) 
   { 
        error("Input password please!"); 
        return; 
      } 
      //Call .NET XML WebServices 
      Thread t = new Thread()  
   {  
   //一定要新开线程,避免锁定屏幕 
        public void run() 
  { 
          try 
    { 
            boolean loginResult =  
   service.login(sLoginUserID, sLoginPwd); 
   //Method:Login() 
            if (loginResult == false) 
   { 
              error("You have no permission login."); 
            } 
            else 
   { 
              display.setCurrent(MainForm); 
            } 
          } 
          catch (Exception e) 
    { 
            if (!EXIT_STRING.equals(e.getMessage())) 
   { 
              e.printStackTrace(); 
              error("Connection problems.\n" 
                    + "Check your internet/proxy settings."); 
            } 
          } 
        } 
      }; 
      t.start(); 
    } 




到此,你就已经完成了J2ME call the dotnet Web Services!让您有非同一般的感觉吧! 
[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码