Search code examples
androidweb-servicesparsingksoap2

Is there a better way of parsing SoapObject in Android


So far I've managed to connect to a Web Service and get my SoapObject response, but I'm having trouble narrowing down a good, clean way of parsing. Searching the Internet hasn't really yielded much results, everyone has a different way and different process usually geared towards their own Web Service making it a one time use type of solution. Basically, below is the response that I get from a Web Service

anyType{
 schema=anyType{
  element=anyType{
   complexType=anyType{
    choice=anyType{
     element=anyType{
      complexType=anyType{
       sequence=anyType{
        element=anyType{}; 
           element=anyType{}; 
            element=anyType{}; 
              element=anyType{}; }; }; }; }; }; }; };   
      diffgram=anyType{
         NewDataSet=anyType{
          Rep_x0020_Information=anyType{
            Login=CorrectLogin; Password=InCorrectPass; }; }; }; }

And basically I want to be able to parse out just the two important fields (Login and Password). From what I read I tried just iterating through the SoapObject response based on the property count but that doesn't seem to work. When I tried I get a property count of 2 for the response so instead I ended up doing some thing like this below:

SoapObject response=(SoapObject) envelope.getResponse();

                if  (response != null) {
                    Log.i("Message", "the response contains: " + response.toString());
                    SoapObject diffgram = (SoapObject) response.getProperty("diffgram");
                    SoapObject NewDataSet = (SoapObject) diffgram.getProperty("NewDataSet");
                    SoapObject RepInfo = (SoapObject) NewDataSet.getProperty("Rep_x0020_Information");
                    for (int i = 0; i < RepInfo.getPropertyCount(); i++) {
                        PropertyInfo info = new PropertyInfo();
                        RepInfo.getPropertyInfo(i, info);
                        Log.d("Info", info.name + " : " + RepInfo.getProperty(i).toString());
                    }


 //which gives the following message in LogCat
  D/Info(716): Login : InCorrectLogin
  D/Info(716): Password : InCorrectPass

This process works, as by the last loop I get the two objects I want but I just feel like there's a cleaner way of doing this. I only ask because as I get further into this App there will be some more complex Web Service calls being made and I want to be able to have something that is reusable throughout the app instead of having to build several SoapObjects for each request just to get down to the objects I want.


Solution

  • There are 2 ways to parse object from web service:
    First, assume you are a web service owner or creator, you can make web service return a Json string, then from client, you can create an entity map to Json, and Json library will take care parsing for you.

    Second: web service usually response many complex objects, so you can make entities to map with these objects.
    You can follow this tutorial to understand how to map a simple object, then go to my answer from this question to know parsing the complex object.
    It's just something like this:

     HttpTransportSE androidHttpTransport = new HttpTransportSE(SERVER_URL);
     androidHttpTransport.call(SOAP_ACTION, envelope);// call
     Entity entity = (Entity)envelope.bodyIn;