Search code examples
androidsoapksoap2

KSOAP 2 with complex objects


I have a problem with KSOAP2 for android with dependencies on an Android device.

I followed the instruction from seesharpgears http://seesharpgears.blogspot.com/2010/10/ksoap-android-web-service-tutorial-with.html

I have a Embarcadero C++ Builder XE2 Server with this function to pass a complex data type.

The function is this:

I tried to create a dummy method witch returns back his request.

I created a class called Category.

But I don't know why, I am receiving still this data with data type vector:

Here my code:

public void getSettings(String sessionId){
 SoapObject Request = new SoapObject(ProgramSettings.NAMESPACE, "GetCategoryById");
  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  envelope.dotNet = false;
  envelope.setOutputSoapObject(Request);
  Category C = new Category();
  envelope.addMapping("urn:@:TrackNavMobile", "Category",new Category().getClass());
 // addMapping("urn:@:TrackNavMobile", "Category", cat.getClass());
  String url="http://" + readIp() + ":" + readPort() +"/soap/ITrackNavMobile";
  HttpTransportSE  androidHttpTransport = new HttpTransportSE(url);
  /*
   * Call the web service and retrieve result ... how luvly <3
   * 
   * */
  try
  {
      androidHttpTransport.call(ProgramSettings.SOAP_ACTION, envelope);
      SoapObject response = (SoapObject)envelope.bodyIn;

      //TextView tv = (TextView)findViewById(R.id.textView1);

         for(int i=0;i<response.getPropertyCount();i++)
   {
             new AlertDialog.Builder(this) 
             .setMessage(response.getProperty(0).toString())
             .setNeutralButton("OKi", null)
             .show();

      //if complex type is present then you can cast this to SoapObject and if primitive type is returned you can use toString() to get actuall value.
   }

  }
  catch(Exception e)
  {
      e.printStackTrace();
  }
}    

This is the result with properties...When I am debugging it calls the setCategory method written in Java with the correct values...but it doesn't return them.. [Category : it.comtec.Category@44f02ea0, return : it.comtec.Category@44f02ea0]

This is my server dummy method witch is called:

 Category*  TTrackNavMobileImpl::GetCategoryById(){
        Category* setup= new Category();
        setup->Name="Thomas";
        setup->CategoryId=1;
        setup->Description="Test1";
        return setup;
 } 

This is the RequestDump:

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:d="http://www.w3.org/2001/XMLSchema" 
 xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"   
 xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header />
  <v:Body>
    <n0:GetCategoryById id="o0" c:root="1"    
     sessionId="FFEF196A3940136D7C141C4F01965D7D" 
     xmlns:n0="http://tempuri.org/" />
  </v:Body>
</v:Envelope>

And this is the ResponseDump:

 <?xml version="1.0"?>
   <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"   
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"   
    xmlns:NS1="urn:TrackNavMobile-ITrackNavMobile">
    <NS1:GetCategoryByIdResponse xmlns:NS2="urn:@:TrackNavMobile">
     <NS2:Category id="1" xsi:type="NS2:Category">
       <Name xsi:type="xsd:string">Thomas</Name>
       <Description xsi:type="xsd:string">Test1</Description>
       <CategoryId xsi:type="xsd:int">1</CategoryId>
      </NS2:Category><return href="#1"/> 
    </NS1:GetCategoryByIdResponse>
  </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Someone has an Idea?


Solution

  • You have to use getResponse() instead of bodyIn. More details are on various blog posts linked on the site as well as directly on the wiki. You will have to implement the parsing of the response either manually or with marshalling.