截至2005年2月6日,還沒有出現支持JSR172的手機!經過大量的試驗證明,目前最方便、快速的方式就是通過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();
}
OK,到此,你就搞定了!
J2ME call the dotnet Web Services!
簡單吧!
(出處:http://www.survivalescaperooms.com)
新聞熱點
疑難解答