【发布时间】:2011-06-22 07:08:47
【问题描述】:
我用 Java 编写了一个 Web 服务,该 Web 服务运行正常,但我不知道如何通过它传递自定义对象列表。这是我的类对象:
public class Contacts{
private int id;
private String username;
private String location;
private Date updated_at;
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setLocation(String location) {
this.location = location;
}
public String getLocation() {
return location;
}
public void setUpdated_at(Date updated_at) {
this.updated_at = updated_at;
}
public Date getUpdated_at() {
return updated_at;
}
}
这是我查询数据库并返回对象列表的 Web 服务方法。每次我尝试返回一个对象时,我都会收到一个axis2错误:[ERROR] org.apache.axis2.AxisFault: Mapping qname not fond for the package: wtp
无论如何,这是我的方法:
public String getUsers()
{
ResultSet mainRS = null;
String username = "root";
String password = "ticket";
String tablename = "users";
String fieldname = "*";
String query = "SELECT " + fieldname + " FROM " + "android." + tablename + ";";
ArrayList<Contacts> lstc = new ArrayList<Contacts>();
/* this chnk of code can appear more or less verbatim */
/* in your database apps (including JSPs) */
String url = "jdbc:mysql://"my IP address":3306/android";
String test = " ";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection(url, username, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
mainRS = rs;
while (rs.next()){
Contacts cont = new Contacts();
if (!(rs.getString("username") == null)){
cont.setUsername(rs.getString("username"));
test = rs.getString("username");
}
if (!(rs.getString("location") == null)){
cont.setLocation(rs.getString("location"));
}
if (!(rs.getString("updated_at") == null)){
cont.setUpdated_at(rs.getDate("updated_at"));
}
if (!(rs.getString("idUsers") == null)){
cont.setId(rs.getInt("idUsers"));
}
lstc.add(cont);
}
rs.close();
stmt.close();
con.close();
}catch(Exception e){
test = e.toString();
}
return test;
}
我在 Java 中使用 Eclipse Galileo,Axis2。任何关于如何传递这些对象的想法都将不胜感激。或者也许,甚至比低音自定义对象更好的方法。我对任何想法持开放态度。
谢谢!
编辑:我编辑了要使用的方法:
Contacts lst[] = new Contacts["count rows here"];
但是,它返回为空的对象。例如:
数据库中有 4 行,所以结果是:
SoapObject: [result:null, result:null, result:null,result:null]
有什么想法吗?
【问题讨论】:
-
我用似乎让我更接近结果的代码编辑了上面的帖子。但是,无论如何,我都会收到空值。
标签: java android mysql web-services axis2