从AXIS2 WebService接收包含ksoap2(Android)中的对象列表/数组的对象

时间:2012-09-05 13:13:46

标签: android web-services ksoap2

我希望有人可以帮助我,因为这个问题花了很多时间,我不知道解决方案。很多提前... 我尝试调用我的AXIS2-Webservice,它返回一个这样的对象:

public class TestObject{

private int zahl = 1;
private String name = "test";
SecondObject sec_object = new SecondObject("eins");
List <SecondObject> obj_list = new ArrayList<SecondObject>();
static SecondObject [] array;

public TestObject(){
    obj_list.add(new SecondObject("zwei"));
    obj_list.add(new SecondObject("drei"));
    array = obj_list.toArray(new SecondObject[2]); }
    //getter & setter

public class SecondObject {

private String wert;

public SecondObject(String wert) {
    this.wert = wert;
}

从Android我称之为:

androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();

int count = response.getPropertyCount();
        Log.v(TAG, "count :"+count);
        for (int y = 0; y < count; y++){
            if (response.getProperty(y)instanceof SoapObject){
                SoapObject sobj = (SoapObject) response.getProperty(y);
                Log.v(TAG, "soapOBJ "+sobj);
                Log.v(TAG, "count "+sobj.getPropertyCount());
                if (sobj.getProperty(0)!= null){
                    Log.v(TAG, "!=NULL"+sobj.getProperty(0)+"");
                }   
            }
        }

SoapObject array = (SoapObject) response.getProperty("array");
Log.v(TAG, "array "+array);

我看到很多帖子,只调用一组对象。在这种情况下,我想调用一个包含对象数组的对象......问题是:数组只包含一个元素,请参阅此日志:

09-05 14:58:31.120: V/ASYNCTASK(9314): count :7
09-05 14:58:31.120: V/ASYNCTASK(9314): soapOBJ SecondObject{wert=zwei; }
09-05 14:58:31.120: V/ASYNCTASK(9314): count 1
09-05 14:58:31.120: V/ASYNCTASK(9314): !=NULLzwei
09-05 14:58:31.120: V/ASYNCTASK(9314): soapOBJ SecondObject{wert=drei; }
09-05 14:58:31.120: V/ASYNCTASK(9314): count 1
09-05 14:58:31.120: V/ASYNCTASK(9314): !=NULLdrei
09-05 14:58:31.120: V/ASYNCTASK(9314): soapOBJ SecondObject{wert=zwei; }
09-05 14:58:31.120: V/ASYNCTASK(9314): count 1
09-05 14:58:31.120: V/ASYNCTASK(9314): !=NULLzwei
09-05 14:58:31.120: V/ASYNCTASK(9314): soapOBJ SecondObject{wert=drei; }
09-05 14:58:31.120: V/ASYNCTASK(9314): count 1
09-05 14:58:31.120: V/ASYNCTASK(9314): !=NULLdrei
09-05 14:58:31.120: V/ASYNCTASK(9314): soapOBJ SecondObject{wert=eins; }
09-05 14:58:31.120: V/ASYNCTASK(9314): count 1
09-05 14:58:31.120: V/ASYNCTASK(9314): !=NULLeins
09-05 14:58:31.120: V/ASYNCTASK(9314): array SecondObject{wert=zwei; }

1 个答案:

答案 0 :(得分:0)

您可以从顶级SoapObject获取单个节点,如下所示:

SoapObject response = (SoapObject) envelope.getResponse();
String name =  response.getPrimitivePropertySafelyAsString(NODE_NAME_FOR_NAME /* e.g. "name"? */);
int zahl = Integer.parseInt(response.getPrimitivePropertySafelyAsString(NODE_NAME_FOR_ZAHL /* e.g. "zahl"? */));
SoapObject obj_list = (SoapObject) response.getPropertySafely("obj_list");
for (int idx = 0; idx < obj_list.getPropertyCount(); idx++) {
    SoapObject secondObject = (SoapObject) forecastResultsNode.getProperty(idx);

    String wert=  forecastNode.getPrimitivePropertySafelyAsString("wert");
}

如果您可以发布实际SOAP响应的片段(以XML格式),那将是一件好事,因此我可以提供更具体的答案。

可以在this blog article中找到解析类似于您所描述的结构的响应的示例。