开发者

Handling Vectors with Ksoap2 in Android

As part of my project I need to access a web service in Java(Axis) using ksoap2. That web service will return a vector Of Customer objects.My problem is I cannot handle that vector in Ksoap2 in Android.I tried many ways. Not getting how to do that. I read about marshalling in ksoap2. Is 开发者_开发百科that needed to solve my problem.If so how can I marshal a Vector in Ksoap2. Somebody please help me.......


In my project, i parsed the object from the ksoap2 answer manually, as described here: http://seesharpgears.blogspot.com/2010/10/web-service-that-returns-array-of.html

Basically you would iterate over your SoapObject you received as answer and build your Customer objects.

Category[] categories = new Category[soap.getPropertyCount()];
    for (int i = 0; i < categories.length; i++) {
        SoapObject pii = (SoapObject)soap.getProperty(i);
        Category category = new Category();
        category.CategoryId = Integer.parseInt(pii.getProperty(0).toString());
        category.Name = pii.getProperty(1).toString();
        category.Description = pii.getProperty(2).toString();
        categories[i] = category;
    }

The code is taken from seesharpgears, credit goes there. If you still have problems with parsing your response, more detail would be needed to help further (eg. xml-response...)


If you register the class of the complex objects that you want to get (the items of the vectors), ksoap2 will try to convert then to give you the vector with objects of the registered class instead of a vector containing SoapObjects. For registering the class with ksoap it is necessary that your class implements KvmSerializable that allows to deserialize the objects in the response by accessing the properties and assigning the values retrieved from the web service. Deppending on your class you might need to also register some Marshals (for example for deserializing int, Double and Float fields of your class).

The registration can be done by:

envelope.addMapping(namespace, typeName, clazz);

Where namespace id the namespace of the type (as can be found in the wsdl), the typename is the name of the type (also shown in the wsdl) and clazz is class that you want to register.

Hope this helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜