Java2 ME:使用 Web 服务
我目前正在使用 NetBeans IDE,因此我想托管一个 WebService 并通过 J2ME 使用它。一切都很顺利(从编写 Web 服务类、部署 Web 服务器、获取 WSDL 的 URL、创建 Java ME Web 服务客户端,然后最终调用方法)。
目前,我的 WebMethod login()
如下所示:
@WebMethod(operationName = "login")
public boolean login(@WebParam(name = "username") String username, @WebParam(name = "password") String password) {
boolean result = false;
System.out.println(username + password);
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, dbUsername, dbPassword);
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT COUNT(*) FROM Account WHERE Username = "
+ "'" + username + "' AND Password = '" + password + "'");
res.next();
int i = res.getInt(1);
if (i == 1) {
result = true;
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
因此,我转到 J2ME 应用程序,创建 Java ME Web 服务客户端,输入此类的 WSDL,然后将这行代码写入调用网络服务:
FirefighterService service = new FirefighterSerivce_Stub();
try {
if (service.login("admin", "admin"); {
System.out.println("success"); }
else {
System.out.println("failure"); }
} catch (Exception e) {
e.printStackTrace();
}
它应该工作。我已经在 Java 控制台应用程序中测试了我的代码行,它运行得很好(连接到数据库,选择,然后打印结果)。结果应该是“成功”。但是当我通过网络服务调用它时,我总是得到“失败”...
如果有帮助,我引用了本教程中的很多内容。 http://nandokakimoto.wordpress.com/ 2009/03/15/creating-a-j2me-web-service-client/
有人知道我可能在哪里搞砸了吗? :(
I'm currently using NetBeans IDE, so I want to host a WebService and consume it via J2ME. Everything goes well (from writing the web service class, deploying the web server, getting the URL for the WSDL, creating the Java ME Web Service Client, then finally calling the methods).
Currently, my WebMethod login()
looks like this:
@WebMethod(operationName = "login")
public boolean login(@WebParam(name = "username") String username, @WebParam(name = "password") String password) {
boolean result = false;
System.out.println(username + password);
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, dbUsername, dbPassword);
Statement st = conn.createStatement();
ResultSet res = st.executeQuery("SELECT COUNT(*) FROM Account WHERE Username = "
+ "'" + username + "' AND Password = '" + password + "'");
res.next();
int i = res.getInt(1);
if (i == 1) {
result = true;
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
So I go over to my J2ME app, create the Java ME Web Service Client, enter the WSDL for this class, and I write this line of code to call the web service:
FirefighterService service = new FirefighterSerivce_Stub();
try {
if (service.login("admin", "admin"); {
System.out.println("success"); }
else {
System.out.println("failure"); }
} catch (Exception e) {
e.printStackTrace();
}
It SHOULD work. I have tested my line of code in a Java console application, and it worked perfectly (connecting to the database, SELECTing, and then printing the result). The result should be "success". But when I call it via the web service, I always get "failure"...
If it helps, I referenced a lot from this tutorial. http://nandokakimoto.wordpress.com/2009/03/15/creating-a-j2me-web-service-client/
Does anyone have any clue as to where I possibly screwed up? :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系...我忘记将我的驱动程序包含到我的网络应用程序中。我很糟糕,我知道。
It's alright... I forgot to include my driver into my web app. I suck, I know.