java - Ksoap简单数组android

标签 java android web-services soap ksoap

我正在尝试从返回 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();
        }
    }

我可以从原始类型检索数据,但这有点复杂。 这是网络服务的响应

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

在将属性添加到列表之前,我还要确保该属性不为空。

这对你有用吗?

关于java - Ksoap简单数组android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8841938/

相关文章:

java - 何时以及如何使用 hibernate 二级缓存?

java - Android:转换为 Dalvik 格式失败:无法执行 dex:null

android - 使用改造 ResponseBody 的自定义消息错误

asp.net - 什么是 DefaultWsdlHelpGenerator.aspx?

web-services - .NET 4.5 中的 HttpResponseMessage 发生了什么,有替代方案吗?

java - 如何调用一个类(在另一个类中)

java - CXF - 如何使用 CXF 3.2.6 正确配置 Spnego

android - Android心电图

java - 将传感器数据保存到android中的文件

ruby-on-rails - 来自地理位置的时区