2011-02-06 98 views
2

我用Java編寫了一個Web服務,並且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;    
     } 

我使用Eclipse伽利略,Axis2的Java編寫的。任何想法如何傳遞這些對象將不勝感激。或者,也許,甚至有更好的方法來做到這一點,然後低音自定義對象。我願意接受任何想法。

謝謝!

編輯:我編輯使用的方法:

Contacts lst[] = new Contacts["count rows here"]; 

但是,它將返回空對象。例如:

有4行中的數據庫中,以便其結果是:

SoapObject:結果:空,結果是:空,結果是:空,結果是:空]

任何想法?

+0

我編輯上面的帖子與代碼似乎讓我更接近結果..但是,無論如何,我收到空值。 – ninnemannk 2011-02-06 17:59:58

回答

0

編輯的方法確實有效..我不得不使用返回類型Contacts []將返回類型設置爲Array of Contacts。隨着:

Contacts contact[] = new Contacts[Count rows here]; 

工作,因爲它應該。編輯完這個^^之後,問題是我如何加載對象。正確的方法是:記住它在一段時間(ResultSet.next)

int num = 0; 
Contacts cont = new Contacts(); 
cont.setId = "id"; 
cont.setLocation = "location"; 
//finish filling your object 
contact[num] = cont; 
num = num + 1; 

謝謝!

0

我還沒有使用Axis2進行Web服務,但我使用了XFire和CXF。有了這些框架,您必須使用XmlBeans或JAXB標記正確的註解來標記域對象。 然後你配置框架的servlet使用你最喜歡的Xml庫(XmlBeans/JAXB)。

相關問題