[英]Ksoap on android— get a list response from a java web service
我有一個java web服務(肥皂),我想使用Android客戶端,因為我正在使用ksoap。
我的網絡服務提供了如下答案:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:listealbumResponse xmlns:ns2="http://ws/">
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:album">
<annee>2008</annee>
<id>6</id>
<titre>Ninja Tuna</titre>
</return>
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:album">
<annee>2008</annee>
<id>10</id>
<titre>Fine Music, Vol. 1</titre>
</return>
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:album">
<annee>2004</annee>
<id>14</id>
<titre>Bob Acri</titre>
</return>
<return xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:album">
<annee>2009</annee>
<id>54</id>
<titre>Rated R</titre>
</return>
</ns2:listealbumResponse>
</S:Body>
</S:Envelope>
這是一個對象列表
要調用我的Web服務,我使用以下代碼:
try{
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = false;
envelope.setOutputSoapObject(Request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject)envelope.getResponse();
當我測試我的響應“結果”時,它只有一個對象,我怎么能得到所有列表並解析它?
問題是,當我解析soap響應時,我只獲得了列表的第一個對象,所以我改變了這一行:
SoapObject result = (SoapObject)envelope.getResponse();
用:
SoapObject result = (SoapObject)envelope.bodyIn;
我得到了所有的清單,我加上這個
testValues = new String[result.getPropertyCount()];
for(int i= 0; i< result.getPropertyCount(); i++){
testValues[i] = result.getProperty(i).toString();
}
祝你好運,謝謝Janusz
碼:
SoapObject result = (SoapObject)envelope.bodyIn;
String output = "";
for(int i= 0; i< result.getPropertyCount(); i++){
SoapObject object = (SoapObject)response.getProperty(i);
output += "annee : " + object.getProperty("annee") + "\n";
output += "id : " + object.getProperty("id") + "\n";
output += "titre : " + object.getProperty("titre") + "\n";
}
結果是單個SoapObject,但是這個對象應該具有您請求的列表中每個項目的屬性。 您可以執行以下操作來檢索所有項目:
private static List parseLists(List listItems, SoapObject response) {
int propertyCount = response.getPropertyCount();
for (int currentProperty = 0; currentProperty < propertyCount; currentProperty++) {
Object input = response.getProperty(currentProperty);
Object result = parseObject(input.toString());
if (result != null) {
listItems.add(result);
}
}
return listItems;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.