2011-02-11 53 views
1

我嘗試製作我的第一個android應用程序! 我使用kso​​ap2調用web服務來獲取我的帳戶的詳細信息。 我有一個函數返回一個數組(resultRequestSOAP)。 在resultRequestSOAP中有一個ARRAY對象。 下面我的功能:需要幫助來顯示從webservice返回的SOAP請求

public Array listall(String session){ 
    final String SOAP_ACTION = "http://www.nubio.net/soap/vps#listAll"; 
    final String METHOD_NAME = "listAll"; 
    final String NAMESPACE = "http://www.nubio.net/soap/vps"; 
    final String URL = "http://www.nubio.net/soap/vps"; 
    Array myArray = null; 
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
    //SoapObject 
    request.addProperty("session",session); 
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
    envelope.setOutputSoapObject(request); 
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
    try 
     { 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      resultRequestSOAP = envelope.getResponse(); 
      //retour = resultRequestSOAP.toString(); 
      if(resultRequestSOAP != null){ 
       myArray = (Array)resultRequestSOAP; 
      } 
     } 
    catch (Exception aE) 
     { 
    aE.printStackTrace();; 
     } 
    return myArray; 

} 

我測試函數返回字符串,它工作正常,但我需要在屏幕上顯示的陣列。 如何顯示resultRequestSOAP中的數組; ? 但是resultRequestSOAP中soap的原始返回值;是:

array(
    0 => Object{ 
    vps_id  : int 
    ip   : string 
    hostname : string 
    password : string (optional) 
    os   : string 
    os_arch  : integer 
    os_distri : string 
    expire  : string (DATE TIME) 
    }, 
    ... 
) 

所以我可以從肥皂中返回數組並顯示它!? 對不起我的英語,我希望你能幫助我:) 最適合我的只會顯示數組中的「主機名字符串」,因爲按鈕有可能嗎?

+0

什麼語言和平臺? – 2011-02-11 14:33:28

+0

@John:這是Java和Android平臺 – 2011-02-11 17:33:08

+0

是的,它和Android平臺,你能幫我嗎?對不起,我的英文..你明白我想說明什麼嗎?感謝您的幫助 – naturlight 2011-02-11 18:00:15

回答

0

不是真的知道什麼你問,但如果你的方法返回對象的數組,你可以打印出對象像這樣:

for(int i=0; i < myArray.length; i++){ 
     Object item = myArray[i]; 
     // Put into a TextView here if you wish, just printing to console 
     System.out.println("Item: "+i +" IP: "+item.ip); 
     // or if it is private with a getter 
     System.out.println("Item: "+i +" IP: "+item.getIp()); 

     System.out.println("Hostname: "+item.hostname); 

     System.out.println("Hostname: "+item.getHostname()); 
} 

我想,讓你的想法呢?

0

這篇博客文章可能有幫助:http://seesharpgears.blogspot.com/2010/10/web-service-that-returns-array-of.html

看到的最後一個方法。

編輯:

,而不是鑄造陣列您resultRequestSOAP,你應該得到的屬性與resultRequestSOAP.getPropertyCount()通過他們的數量,循環獲取每個對象:從這些對象

SoapObject pii = (SoapObject)resultRequestSOAP.getProperty(i);

你可以提取自己的屬性(vps_id,ip,hostname ...):

vps_id = Integer.parseInt(pii.getProperty(0).toString());

等等。在這個例子中,他們正在構建他們的Category對象的數組,但是當然你可以使用你想要的數據。