簡體   English   中英

Ksoap簡單數組Android

[英]Ksoap simple array android

我正在嘗試從返回Strings數組的Web服務中檢索數據。 我做不到,所以給你一小段代碼
請幫助我,我快瘋了!

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> 

提前致謝

您應該能夠使用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));
}

在將屬性添加到列表之前,我還確保該屬性不為null。

這對您有用嗎?

這行:

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

應該更改為:

SoapObject response = (SoapObject)envelope.bodyIn;

使用:

response.getProperty(Index);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM