【问题标题】:Ksoap simple array androidKsoap简单数组android
【发布时间】:2012-01-12 20:37:38
【问题描述】:

我正在尝试从返回字符串数组的 Web 服务中检索数据。 我做不到,所以我给你一个 sn-p 代码
请帮帮我,我快疯了!

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> 

提前致谢

【问题讨论】:

  • 如果您得到解决方案,请在此处发布 ..... 检查此链接可能对您有帮助 .. ksoap.objectweb.org/project/faq/index.html ... 根据他们的说法,发送和接收必须通过使用向量来完成..但即使我可以实现它..所以如果你有请分享解决方案..

标签: java android web-services soap ksoap


【解决方案1】:

您应该能够使用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));
}

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

这对你有用吗?

【讨论】:

  • 我无法看到日志消息以确保我正在接收数据所以第一条日志消息正在运行有什么问题我写它以确保函数正在运行但第二条是不是。谢谢你帮助我,请告诉我wtf?
  • 调试函数时会发生什么?在HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 行之后添加androidHttpTransport.debug = true;。然后,在调试模式下,您可以在androidHttpTransport.call(SOAP_ACTION, envelope); 之后检查您的androidHttpTransport 中的responseDump 和requestDump,以查看XML 格式的请求和响应。
【解决方案2】:

这一行:

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

应该改为:

SoapObject response = (SoapObject)envelope.bodyIn;

使用:

response.getProperty(Index);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多