【问题标题】:Get a List of array from webservice in android从android中的webservice获取数组列表
【发布时间】:2013-09-25 09:17:27
【问题描述】:

我有一个来自 PHP Web 服务的对象数组,其内容如下:

anyType[
objectname1{element1=1234567890; element2=test; element3=1110; element3=72.824043; },
objectname1{element1=11090999; element2=test; element3=2292; element3=72.824043; }]

我正在使用 ksoap2 在我的 android 应用程序中获取 web 服务数据。

我尝试过使用question 中的chadi cortbaoui's 解决方案,但它给出了错误

java.lang.ClassCastException: java.util.Vector 无法转换为 org.ksoap2.serialization.SoapObject

对于SoapObject response1 = (SoapObject) response.getProperty(0); 行,代码如下

envelope.encodingStyle = "UTF-8";
httpTransport.debug = true;
httpTransport.call(SOAP_ACTION, envelope);
response =  (SoapObject) envelope.bodyIn;
SoapObject response1 = (SoapObject) response.getProperty(0);

我尝试过使用SoapPrimitive 代替SoapObject,我也尝试过使用envelope.getResponse() 代替envelope.bodyIn,但它不起作用,它会引发同样的错误。

【问题讨论】:

  • @SamirMangroliya:这有什么关系?你的帖子是关于 json 的。这个问题不是。
  • @SamirMangroliya 在我的情况下我应该如何使用你的帖子?如果我可以在我的问题中使用帖子,你能帮我吗?我尝试使用您帖子中的代码,但它在字符 12 处为未终止数组引发错误,即“=”在我的字符串数组中签名?
  • @SamirMangroliya 我解决了我的问题,您的链接也没有帮助,因为我收到的响应不是 JSON 字符串。无论如何感谢您的帮助。:)

标签: php android web-services android-ksoap2


【解决方案1】:

我在question 的帮助下使用以下代码解决了我的问题

httpTransport.call(SOAP_ACTION, envelope);
response =  (SoapObject) envelope.bodyIn;
Vector<?> responseVector = (Vector<?>) response.getProperty(0);//store the response object values in a Vector(It solved the vector error which i was getting

int count=responseVector.size();

for (int i = 0; i <count; ++i) { //for loop for the array of the result    
SoapObject test=(SoapObject)responseVector.get(i);
String value1 = test.getProperty("value1").toString();//get Your values from the soap object
String value2 = test.getProperty("value2").toString();
String value3 = test.getProperty("value3").toString();
String value4 = test.getProperty("value4").toString();
  /*thats it now add the received values to your list using the for loop*/
}

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-08
    • 1970-01-01
    • 2013-02-08
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    相关资源
    最近更新 更多