Click here to Skip to main content
13,046,087 members (66,873 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 21 Aug 2014

Android:deserialize KSoap2 response into Complex object or arrays purely

, 21 Aug 2014
Rate this:
Please Sign up or sign in to vote.
Library for deserializing the Ksoap2 response .


This project is aimed to help you deserialize KSoap2 response into complex object (Class in your project) and ArrayList by using my class file.


KSoap2 is really a good library for us to use web service on android devices.However,I found it hard to parse or deserialize the result into my own class objects or arrays like(ArrayList<E>).I write the library to do all of this for deserializing the response.

You can receive the result like

MyClass myClassObect=new MyClass(SoapObject response);//response is the result from Web service

Using the code

Download and add the java file into your own Package and then alter the packge name of your own!!!

Inside the java file is a class called 'Deserialization' ,it has only one generic method called 'SoapDeserialize' which is used to deserialize the response.Signaniture is defined below:

// the first parameter item is the variable of your class T
//The second parameter object is the Soap context(named by me :-)) which is actually an instance of SoapObject.
public  <T> void SoapDeserialize(T item,SoapObject object){

Say we have a class Person.

public class Person{
String name;
int age;
ArrayList<Person> children;//include other person's info


The response(toString) from webservice may look like this:

AnyType={name=Iron Man,age=30,children=AnyType{AnyType={name=IronDaughter....},AnyType={name=IronSon....}... }}

Such a complex structure,huh?

What we need to do is just adding a constructor for our class Person.

public class Person{
String name;
int age;
ArrayList<Person> children;//include other person's info
public Person(SoapObject object){
new Deserialization().SoapDeserialize(this,object);

//'this' is the instance itself ,
//object is the SoapObject received from response when we call new Person(soapobject)


What we need to do is just to hand the job to Deserialization class and it will do it for us."yourURL", yourEnvelop);//call the web service
          if (yourEnvelop.getResponse()!=null) {      
                //if receive only one object
                Person person=new Person((SoapObject)envelope.getResponse());
               //receive array of objects,directly call SoapDeserialize()
               // the first parameter is the Class of the objects in array the second parameter is soapObject ,the same as above
               // This method return an array which is the result we need 
                 ArrayList<Person> arrayList=new ArrayList<Person>();
                arrayList= new Deserialization().SoapDeserialize(Person.class,(SoapObject)(envelope.getResponse()));

From my test ,all types below can be identified and dealed by my class.

  • int,Integer
  • Double
  • String
  • Float
  • Complex Class (As long as you declare the constructor I mention before)
  • ArrayList<T> (Which should work for ArrayList raw type ,too.Haven't tested for Vector type)
  • I haven't added Enum type,but if you want,it's easy if you understand the code inside my file.

Remember:Every complex Object class always should declare the constructor for being dealed by the deserialization tool.Moreover,don‘t forget the declaration in class T of ArrayList<T> because you may not directly call this to instance this,but this is called from the deserialization tool.

How I make it work:

Use reflect to get your class's fields and determine which type it belong to by Field.getType method.

For ArrayList,I do it in the same way but dynamicaly instantiate the variable for every object in SoapObject like

AnyType={name=Iron Man,age=30,children=AnyType{AnyType={name=IronDaughter....},AnyType={name=IronSon....}... }}

and add them to a list(Arraylist variable) ,this process is actually a recursion. Then use Field.set(person,list) to set list to the actual field.

Feel free to download my code and alter it as you want.

If you have any problem ,just feel free to let me know .



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Little Carl
Software Developer (Senior) Gudu
China China
I enjoy WPF programing and Node.js development.I'm living in Shanghai,China.

You may also be interested in...


Comments and Discussions

QuestionIs Support still available Pin
Craig Shields7-Feb-16 5:33
memberCraig Shields7-Feb-16 5:33 
QuestionPlease provide complete sample implementation. Pin
Member 1193393725-Aug-15 3:09
memberMember 1193393725-Aug-15 3:09 
Questionhaving a problem with getting data from ArrayList Pin
waleedquwaider8-Dec-14 1:30
memberwaleedquwaider8-Dec-14 1:30 
GeneralMy vote of 5 Pin
waleedquwaider8-Dec-14 0:57
memberwaleedquwaider8-Dec-14 0:57 
QuestionObjectId Pin
Member 111797187-Dec-14 16:09
memberMember 111797187-Dec-14 16:09 
AnswerRe: ObjectId Pin
Little Carl7-Dec-14 17:17
memberLittle Carl7-Dec-14 17:17 
QuestionMissing decalration object ObjectId Pin
thorr8025-Oct-14 6:22
memberthorr8025-Oct-14 6:22 
AnswerRe: Missing decalration object ObjectId Pin
Member 1123108113-Nov-14 5:41
memberMember 1123108113-Nov-14 5:41 
GeneralRe: Missing decalration object ObjectId Pin
Little Carl7-Dec-14 17:24
memberLittle Carl7-Dec-14 17:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 22 Aug 2014
Article Copyright 2014 by Little Carl
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid