2012-01-12 37 views
0

我試圖從返回字符串數組的Web服務中檢索數據。 我無法做到這一點,所以我給你的代碼片段
請幫助我我要瘋了!Ksoap簡單數組android

public void updateCategories(){ 
     SOAP_ACTION = "http://app.market_helper.com/getCategories"; 
     METHOD_NAME = "getCategories"; 
     Log.i("MESSAGE FROM me", "It's running wtf"); 
     try { 
      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11); 
      envelope.setOutputSoapObject(request); 
      HttpTransportSE androidHttpTransport = new HttpTransportSE(
        URL); 
      androidHttpTransport.call(SOAP_ACTION, envelope); 
      SoapObject response = (SoapObject)envelope.getResponse(); 
     Log.i("message to me",""+response.getPropertyCount()); 
     }catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

我可以從原始類型檢索數據,但這有點複雜。 這是來自Web服務的響應提前

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<soapenv:Body> 
<getCategoriesResponse xmlns="http://DefaultNamespace"> 
    <getCategoriesReturn>desktop computers</getCategoriesReturn> 
    <getCategoriesReturn>laptop computers</getCategoriesReturn> 
    <getCategoriesReturn>mobile phones</getCategoriesReturn> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    <getCategoriesReturn xsi:nil="true" /> 
    </getCategoriesResponse> 
    </soapenv:Body> 
    </soapenv:Envelope> 

感謝

+0

如果u得到解決後在這裏.....檢查此鏈接可能會對你有所幫助。http://ksoap.objectweb.org/project/faq/index.html ...根據他們發送和接收必須通過使用vector.but但即使我cou ld實現它..所以請分享你的解決方案,如果你有.. – 2012-01-13 05:24:14

回答

2

你應該能夠response.getProperty(index).toString()得到的字符串值,或者如果你願意,response.getPropertyAsString(index)(指數也可以用的名稱所取代該物業)。要檢索所有字符串值,請嘗試將它們放入一個將字符串添加到列表中的循環中。

List<String> categories = new ArrayList<String>(); 
int count = response.getPropertyCount(); 

for(int i = 0; i < count; i++) { 
    if(response.getProperty(i) != null) 
     categories.add(response.getPropertyAsString(i)); 
} 

我還確認該屬性在將其添加到列表中之前不爲空。

這是否適合您?

+0

Im無法看到日誌消息,以確保我收到數據 所以有什麼問題那裏第一個日誌消息正在運行我寫它是確定該功能正在運行,但第二個不是。 謝謝你幫助我,請告訴我wtf? – 2012-01-17 17:07:43

+0

當您調試該功能時會發生什麼?在'HttpTransportSE androidHttpTransport = new HttpTransportSE(URL)'行後面添加'androidHttpTransport.debug = true;'。然後,在調試模式下,您可以在androidHttpTransport.call(SOAP_ACTION,envelope)之後檢查您的androidHttpTransport中的responseDump和requestDump,以XML格式查看請求和響應。 – ajgarn 2012-01-18 15:38:42

1

這條線:

SoapObject response = (SoapObject)envelope.getResponse(); 

應改爲:

SoapObject response = (SoapObject)envelope.bodyIn; 

使用:

response.getProperty(Index);