title: KSoap2 and KvmSerializable - How to Send complex Objects like Stringarrays
body: <p>I want to send a complex object from my Ksoap Client to my webservice. It’s an object of my Class Category. The Code is based on the example of this tutorial:</p>
  
  <p><a href="http://seesharpgears.blogspot.de/2010/10/ksoap-android-web-service-tutorial-with.html" rel="nofollow">http://seesharpgears.blogspot.de/2010/10/ksoap-android-web-service-tutorial-with.html</a></p>
  
  <p>The webservice is running and the WSDL-file shows the category object. But if I send a category object to my soap webservice, the following error is showing up:</p>
  
  <blockquote>
    <p>[ERROR] Exception occurred while trying to invoke service method
    getCategoryObject org.apache.axis2.AxisFault: Unknow type
    {http://ws.chathura.com}Category Logcat shows up the following: 06-12
    11:08:57.576: W/System.err(777): SoapFault - faultcode:
    'soapenv:Server' faultstring: 'Unknow type
    {http://ws.chathura.com}Category' faultactor: 'null' detail:
    org.kxml2.kdom.Node@4054a528</p>
  </blockquote>
  
  <p>This is my Java Webservice runnning on Tomcat 7:
  </p>
  
  <pre><code>package com.chathura.ws;
  
  import java.util.Vector;
  
  
  
  public class HelloWorldWS
  {
  
  
   public Category getCategoryObject(Category obj)
   {
    Category C1 = new Category();
  
  
      C1.setCategoryId(1);
      C1.setDescription("server desc");
      C1.setName("server Name");
  
  
      System.out.println(obj.getCategoryId());
  
      return C1;
   }
  }
  </code></pre>
  
  <p>This is my Category Class on the WebServer:</p>
  
  <pre><code>package com.chathura.ws;
  
  public class Category {
   private int CategoryId;
   private String Name;
   private String Description;
   /**
    * @return the categoryId
    */
   public int getCategoryId() {
    return CategoryId;
   }
   /**
    * @param categoryId the categoryId to set
    */
   public void setCategoryId(int categoryId) {
    CategoryId = categoryId;
   }
   /**
    * @return the name
    */
   public String getName() {
    return Name;
   }
   /**
    * @param name the name to set
    */
   public void setName(String name) {
    Name = name;
   }
   /**
    * @return the description
    */
   public String getDescription() {
    return Description;
   }
   /**
    * @param description the description to set
    */
   public void setDescription(String description) {
    Description = description;
   }
  
  }
  </code></pre>
  
  <p>This is the WSDL File:
  </p>
  
  <pre><code>    This XML file does not appear to have any style information associated with it. The document tree is shown below.
  &lt;wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://ws.chathura.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://ws.chathura.com/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://ws.chathura.com"&gt;
  &lt;wsdl:documentation&gt;Please Type your service description here&lt;/wsdl:documentation&gt;
  &lt;wsdl:types&gt;
  &lt;xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ws.chathura.com/xsd"&gt;
  &lt;xs:complexType name="Category"&gt;
  &lt;xs:sequence&gt;
  &lt;xs:element minOccurs="0" name="categoryId" type="xs:int"/&gt;
  &lt;xs:element minOccurs="0" name="description" nillable="true" type="xs:string"/&gt;
  &lt;xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/&gt;
  &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  &lt;xs:schema xmlns:ax22="http://ws.chathura.com/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://ws.chathura.com"&gt;
  &lt;xs:import namespace="http://ws.chathura.com/xsd"/&gt;
  &lt;xs:element name="getCategoryObject"&gt;
  &lt;xs:complexType&gt;
  &lt;xs:sequence&gt;
  &lt;xs:element minOccurs="0" name="obj" nillable="true" type="ax21:Category"/&gt;
  &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;xs:element name="getCategoryObjectResponse"&gt;
  &lt;xs:complexType&gt;
  &lt;xs:sequence&gt;
  &lt;xs:element minOccurs="0" name="return" nillable="true" type="ax21:Category"/&gt;
  &lt;/xs:sequence&gt;
  &lt;/xs:complexType&gt;
  &lt;/xs:element&gt;
  &lt;/xs:schema&gt;
  &lt;/wsdl:types&gt;
  &lt;wsdl:message name="getCategoryObjectRequest"&gt;
  &lt;wsdl:part name="parameters" element="ns:getCategoryObject"/&gt;
  &lt;/wsdl:message&gt;
  &lt;wsdl:message name="getCategoryObjectResponse"&gt;
  &lt;wsdl:part name="parameters" element="ns:getCategoryObjectResponse"/&gt;
  &lt;/wsdl:message&gt;
  &lt;wsdl:portType name="HelloWorldWSPortType"&gt;
  &lt;wsdl:operation name="getCategoryObject"&gt;
  &lt;wsdl:input message="ns:getCategoryObjectRequest" wsaw:Action="urn:getCategoryObject"/&gt;
  &lt;wsdl:output message="ns:getCategoryObjectResponse" wsaw:Action="urn:getCategoryObjectResponse"/&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:portType&gt;
  &lt;wsdl:binding name="HelloWorldWSSoap11Binding" type="ns:HelloWorldWSPortType"&gt;
  &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/&gt;
  &lt;wsdl:operation name="getCategoryObject"&gt;
  &lt;soap:operation soapAction="urn:getCategoryObject" style="document"/&gt;
  &lt;wsdl:input&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;soap:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:binding&gt;
  &lt;wsdl:binding name="HelloWorldWSSoap12Binding" type="ns:HelloWorldWSPortType"&gt;
  &lt;soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/&gt;
  &lt;wsdl:operation name="getCategoryObject"&gt;
  &lt;soap12:operation soapAction="urn:getCategoryObject" style="document"/&gt;
  &lt;wsdl:input&gt;
  &lt;soap12:body use="literal"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;soap12:body use="literal"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:binding&gt;
  &lt;wsdl:binding name="HelloWorldWSHttpBinding" type="ns:HelloWorldWSPortType"&gt;
  &lt;http:binding verb="POST"/&gt;
  &lt;wsdl:operation name="getCategoryObject"&gt;
  &lt;http:operation location="getCategoryObject"/&gt;
  &lt;wsdl:input&gt;
  &lt;mime:content type="text/xml" part="parameters"/&gt;
  &lt;/wsdl:input&gt;
  &lt;wsdl:output&gt;
  &lt;mime:content type="text/xml" part="parameters"/&gt;
  &lt;/wsdl:output&gt;
  &lt;/wsdl:operation&gt;
  &lt;/wsdl:binding&gt;
  &lt;wsdl:service name="HelloWorldWS"&gt;
  &lt;wsdl:port name="HelloWorldWSHttpSoap11Endpoint" binding="ns:HelloWorldWSSoap11Binding"&gt;
  &lt;soap:address location="http://localhost:8080/WebProject_KomplexObjects/services/HelloWorldWS.HelloWorldWSHttpSoap11Endpoint/"/&gt;
  &lt;/wsdl:port&gt;
  &lt;wsdl:port name="HelloWorldWSHttpSoap12Endpoint" binding="ns:HelloWorldWSSoap12Binding"&gt;
  &lt;soap12:address location="http://localhost:8080/WebProject_KomplexObjects/services/HelloWorldWS.HelloWorldWSHttpSoap12Endpoint/"/&gt;
  &lt;/wsdl:port&gt;
  &lt;wsdl:port name="HelloWorldWSHttpEndpoint" binding="ns:HelloWorldWSHttpBinding"&gt;
  &lt;http:address location="http://localhost:8080/WebProject_KomplexObjects/services/HelloWorldWS.HelloWorldWSHttpEndpoint/"/&gt;
  &lt;/wsdl:port&gt;
  &lt;/wsdl:service&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>
  
  <p>And this is my Android-Testing-App:</p>
  
  <pre><code>    package de.bachelor.marcel;
  
      import java.util.Vector;
  
      import org.ksoap2.SoapEnvelope;
      import org.ksoap2.serialization.PropertyInfo;
      import org.ksoap2.serialization.SoapObject;
      import org.ksoap2.serialization.SoapSerializationEnvelope;
      import org.ksoap2.transport.AndroidHttpTransport;
  
      import android.app.Activity;
      import android.os.Bundle;
      import android.widget.TextView;
  
      public class WSClientKomplexeObjekteActivity extends Activity {
  
       private static final String SOAP_ACTION = "http://ws.chathura.com/getCategoryObject";
          private static final String METHOD_NAME = "getCategoryObject";
          private static final String NAMESPACE = "http://ws.chathura.com";
          private static final String URL = "http://192.168.2.102:8080/WebProject_KomplexObjects/services/HelloWorldWS?wsdl";
  
          /** Called when the activity is first created. */
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
  
              SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
              /*
               * Create Category with Id to be passed as an argument
               * 
               * */
              Category C = new Category();
              C.CategoryId = 1;
              C.Description = "Das ist die Beschreibung";
              C.Name = "ObjektName"; 
  
              /*
               * Set the category to be the argument of the web service method
               * 
               * */
  
              PropertyInfo objekt = new PropertyInfo();
              objekt.setName("obj");
              objekt.setValue(C);
              objekt.setType(C.getClass());
  
              Request.addProperty(objekt);
  
              /*
               * Set the web service envelope
               * 
               * */
              SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  
              envelope.setOutputSoapObject(Request);
  
              envelope.addMapping(NAMESPACE, "Category",new Category().getClass());
              AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
              /*
               * Call the web service and retrieve result ... how luvly &lt;3
               * 
               * */
              try
              {
                  androidHttpTransport.call(SOAP_ACTION, envelope);
                  SoapObject response = (SoapObject)envelope.getResponse();
                  C.CategoryId =  Integer.parseInt(response.getProperty(0).toString());
                  C.Name =  response.getProperty(1).toString();
                  C.Description = (String) response.getProperty(2).toString();
                  TextView tv = (TextView)findViewById(R.id.textview1);
                  tv.setText("CategoryId: " +C.CategoryId + " Name: " + C.Name + " Description " + C.Description);
              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
          }
      }
  </code></pre>
  
  <p>And here is the Category Class from Android Client:</p>
  
  <pre><code>package de.bachelor.marcel;
  
  import java.util.Hashtable;
  
  import org.ksoap2.serialization.KvmSerializable;
  import org.ksoap2.serialization.PropertyInfo;
  
  public class Category implements KvmSerializable 
  {
  
   public int CategoryId;
   public String Description;
      public String Name;
  
      public Category(){}
  
  
      public Category(int categoryId, String description, String name) 
      {
  
          CategoryId = categoryId;
          Name = name;
          Description = description;
      }
  
   @Override
   public Object getProperty(int arg0) 
   {
    switch(arg0)
          {
          case 0:
              return CategoryId;
          case 1:
              return Description;
          case 2:
           return Name;
          }
    return null;
   }
  
   @Override
   public int getPropertyCount() 
   {
    return 3;
   }
  
   @Override
   public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo arg2) 
   {
    switch(arg0)
          {
          case 0:
              arg2.type = PropertyInfo.INTEGER_CLASS;
              arg2.name = "CategoryId";
              break;
          case 1:
              arg2.type = PropertyInfo.STRING_CLASS;
              arg2.name = "Description";
              break;
          case 2:
              arg2.type = PropertyInfo.STRING_CLASS;
              arg2.name = "Name";
              break;
          default:break;
          }
  
   }
  
   @Override
   public void setProperty(int arg0, Object arg1) 
   {
    switch(arg0)
          {
          case 0:
              CategoryId = Integer.parseInt(arg1.toString());
              break;
          case 1:
           Description = arg1.toString();
              break;
          case 2:
              Name = arg1.toString();
              break;
          default:
              break;
          }
  
   }
  
  }
  </code></pre>