Introduction
This project is aimed to help you deserialize KSoap2 response into complex object (Class in your project) and ArrayList by using my class file.
Background
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);
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:
public <T> void SoapDeserialize(T item,SoapObject object){
}
Say we have a class Person.
public class Person{
String name;
int age;
ArrayList<Person> children;
}
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;
public Person(SoapObject object){
new Deserialization().SoapDeserialize(this,object);
}
}
What we need to do is just to hand the job to Deserialization
class and it will do it for us.
ht.call("yourURL", yourEnvelop);
if (yourEnvelop.getResponse()!=null) {
Person person=new Person((SoapObject)envelope.getResponse());
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 .
Thanks.