Click here to Skip to main content
11,414,234 members (72,066 online)
Click here to Skip to main content

Android:deserialize KSoap2 response into Complex object or arrays purely

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

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);//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){
//TODO...
}

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.

ht.call("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 .

Thanks.

License

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

Share

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.

Comments and Discussions

 
Questionhaving a problem with getting data from ArrayList Pin
waleedquwaider, 8-Dec-14 2:30
memberwaleedquwaider8-Dec-14 2:30 
GeneralMy vote of 5 Pin
waleedquwaider, 8-Dec-14 1:57
memberwaleedquwaider8-Dec-14 1:57 
QuestionObjectId Pin
Member 11179718, 7-Dec-14 17:09
memberMember 111797187-Dec-14 17:09 
AnswerRe: ObjectId Pin
Little Carl, 7-Dec-14 18:17
memberLittle Carl7-Dec-14 18:17 
QuestionMissing decalration object ObjectId Pin
thorr80, 25-Oct-14 7:22
memberthorr8025-Oct-14 7:22 
AnswerRe: Missing decalration object ObjectId Pin
Member 11231081, 13-Nov-14 6:41
memberMember 1123108113-Nov-14 6:41 
GeneralRe: Missing decalration object ObjectId Pin
Little Carl, 7-Dec-14 18:24
memberLittle Carl7-Dec-14 18:24 

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

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

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