简体   繁体   English

Ksoap简单数组Android

[英]Ksoap simple array android

I'm trying to retrieve data from web service that returns an array of Strings . 我正在尝试从返回Strings数组的Web服务中检索数据。 I couldn't do it so I give you a snippet of code 我做不到,所以给你一小段代码
please help me I'm going crazy! 请帮助我,我快疯了!

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();
        }
    }

I can retrieve data from primitive types but this is going a little bit complex . 我可以从原始类型中检索数据,但这有点复杂。 this is the response from the web service 这是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> 

thanks in advance 提前致谢

You should be able to get the string values with response.getProperty(index).toString() or if you want, response.getPropertyAsString(index) (index can also be replaced with the name of the property). 您应该能够使用response.getProperty(index).toString()来获取字符串值,或者,如果需要,可以使用response.getPropertyAsString(index) (索引也可以用属性名称替换)。 To retrieve all string values, try putting them into a loop which adds the strings into a list. 要检索所有字符串值,请尝试将它们放入循环中,该循环会将字符串添加到列表中。

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));
}

I also make sure that the property is not null before adding it into the list. 在将属性添加到列表之前,我还确保该属性不为null。

Does this work for you? 这对您有用吗?

This line: 这行:

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

should be changed to: 应该更改为:

SoapObject response = (SoapObject)envelope.bodyIn;

to use: 使用:

response.getProperty(Index);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM