Search code examples
androidweb-servicesksoap2

Passing complex object to a web service


I am trying to call a web service from my app. If I invoke web service method which accepts string or primitives as input then it works fine but when invoke a web service method which accepts a complex object (of Specimen class in my case) then I get following error

org.apache.axis2.AxisFault: Unknow type {http://spec.com}specimen
    at org.apache.axis2.databinding.utils.BeanUtil.deserialize(BeanUtil.java:349)
    at org.apache.axis2.databinding.utils.BeanUtil.processObject(BeanUtil.java:827)
    at org.apache.axis2.databinding.utils.BeanUtil.ProcessElement(BeanUtil.java:746)   

I am following : http://seesharpgears.blogspot.in/2010/10/ksoap-android-web-service-tutorial-with.html

I have tested my web service in web service explorer and its working fine.Here is my web service

package com.spec;

public class Spec {

    public String sayHello(String name)   // I can successfully invoke this 
    {
        return "Have a great day " + name;
    }

    public String saveSpecimen(Specimen specimen) // Getting error for this
    {
        System.out.println("id: " + specimen.getId());
        System.out.println("name: " + specimen.getName());
        return "Specimend with id " + specimen.getId() + " is successfully saved";
    }
}

Here is my specimen class

package com.ws;

import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class Specimen implements KvmSerializable {
    int id;
    String name;

    public Specimen() {
    }

    public Specimen(int idValue, String nameValue) {
        id = idValue;
        name = nameValue;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Object getProperty(int arg0) {
        if (arg0 == 0)
            return id;
        else
            return name;
    }

    public int getPropertyCount() {
        // TODO Auto-generated method stub
        return 2;
    }

    public void getPropertyInfo(int arg0, Hashtable arg1, PropertyInfo info) {
        switch (arg0) {
        case 0:
            info.type = PropertyInfo.INTEGER_CLASS;
            info.name = "id";
            break;
        case 1:
            info.type = PropertyInfo.STRING_CLASS;
            info.name = "name";
            break;
        default:
            break;
        }
    }

    public void setProperty(int arg0, Object value) {
        switch (arg0) {
        case 0:
            id = Integer.parseInt(value.toString());
            break;
        case 1:
            name = value.toString();
            break;
        }
    }
}   

Snippet from Activity

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                OPERATION_NAME);

        Specimen specimen = new Specimen();
        specimen.setId(1);
        specimen.setName("Test_Specimen");
        PropertyInfo info = new PropertyInfo();
        info.setName("specimen");
        info.setValue(specimen);
        info.setType(specimen.getClass());
        request.addProperty(info);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        envelope.addMapping(WSDL_TARGET_NAMESPACE, "specimen",new Specimen().getClass());
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
            httpTransport.call(SOAP_ACTION, envelope);
            SoapObject response = (SoapObject)envelope.getResponse();
            Toast.makeText(getApplicationContext(),"Msg from web servce "       +              response.toString(),Toast.LENGTH_LONG).show();

I think the problem is with

 envelope.addMapping(WSDL_TARGET_NAMESPACE, "specimen",new Specimen().getClass());

Here is my Specimen complex type declaration in wsdl file

<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" argetNamespace="http://spec.com/xsd">   
<xs:complexType name="Specimen">  
<xs:sequence>  
<xs:element minOccurs="0" name="id" type="xs:int"/>   
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/></xs:sequence></xs:complexType>  
</xs:schema>

Please let me know if I am making any blunder.

Thanks!


Solution

  • Tried a lot but not able to get it work. So updated web service to simple parameters and passed all attributes present in object.