Search code examples
androidexceptionobjectksoap2

KSoap 2 with Android Throws "Unknown Property:" Exception. I could only RECEIVE or SEND Complex Objects


i'am trying to revice and send complex Object to/from my webservice, but my problem is, that i could do only recive or send without any exceptions.

The Code is based on the example of this tutorial

This is my Java Webservice running on Tomcat7:

public class HelloWorldWS
{   
    public Category GetSumOfTwoInts(Category C )
    {
        Category back= new Category();
        back.setCategoryId(112);
        back.setDescription("server");
        back.setName("myNameFromServer");

        System.out.println("For testing only: " + C.getCategoryId() + C.getDescription());

        return back;
    }
}

This is the generated WSDL file:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://WS.androidroleplay.fk4.de.hs_bremen.de" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ax21="http://WS.androidroleplay.fk4.de.hs_bremen.de/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.androidroleplay.fk4.de.hs_bremen.de">
<wsdl:documentation>Please Type your service description here</wsdl:documentation>
<wsdl:types>
<xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://WS.androidroleplay.fk4.de.hs_bremen.de/xsd">
<xs:complexType name="Category">
<xs:sequence>
<xs:element minOccurs="0" name="categoryId" type="xs:int"/>
<xs:element minOccurs="0" name="description" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
<xs:schema xmlns:ax22="http://WS.androidroleplay.fk4.de.hs_bremen.de/xsd" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://WS.androidroleplay.fk4.de.hs_bremen.de">
<xs:import namespace="http://WS.androidroleplay.fk4.de.hs_bremen.de/xsd"/>
<xs:element name="GetSumOfTwoInts">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="C" nillable="true" type="ax21:Category"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="GetSumOfTwoIntsResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="ax21:Category"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
<wsdl:message name="GetSumOfTwoIntsRequest">
<wsdl:part name="parameters" element="ns:GetSumOfTwoInts"/>
</wsdl:message>
<wsdl:message name="GetSumOfTwoIntsResponse">
<wsdl:part name="parameters" element="ns:GetSumOfTwoIntsResponse"/>
</wsdl:message>
<wsdl:portType name="HelloWorldWSPortType">
<wsdl:operation name="GetSumOfTwoInts">
<wsdl:input message="ns:GetSumOfTwoIntsRequest" wsaw:Action="urn:GetSumOfTwoInts"/>
<wsdl:output message="ns:GetSumOfTwoIntsResponse" wsaw:Action="urn:GetSumOfTwoIntsResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldWSSoap11Binding" type="ns:HelloWorldWSPortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="GetSumOfTwoInts">
<soap:operation soapAction="urn:GetSumOfTwoInts" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="HelloWorldWSSoap12Binding" type="ns:HelloWorldWSPortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<wsdl:operation name="GetSumOfTwoInts">
<soap12:operation soapAction="urn:GetSumOfTwoInts" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="HelloWorldWSHttpBinding" type="ns:HelloWorldWSPortType">
<http:binding verb="POST"/>
<wsdl:operation name="GetSumOfTwoInts">
<http:operation location="GetSumOfTwoInts"/>
<wsdl:input>
<mime:content type="text/xml" part="parameters"/>
</wsdl:input>
<wsdl:output>
<mime:content type="text/xml" part="parameters"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorldWS">
<wsdl:port name="HelloWorldWSHttpSoap11Endpoint" binding="ns:HelloWorldWSSoap11Binding">
<soap:address location="http://localhost:8080/WebProject_DB16/services/HelloWorldWS.HelloWorldWSHttpSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="HelloWorldWSHttpsSoap11Endpoint" binding="ns:HelloWorldWSSoap11Binding">
<soap:address location="https://localhost:8443/WebProject_DB16/services/HelloWorldWS.HelloWorldWSHttpsSoap11Endpoint/"/>
</wsdl:port>
<wsdl:port name="HelloWorldWSHttpsSoap12Endpoint" binding="ns:HelloWorldWSSoap12Binding">
<soap12:address location="https://localhost:8443/WebProject_DB16/services/HelloWorldWS.HelloWorldWSHttpsSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="HelloWorldWSHttpSoap12Endpoint" binding="ns:HelloWorldWSSoap12Binding">
<soap12:address location="http://localhost:8080/WebProject_DB16/services/HelloWorldWS.HelloWorldWSHttpSoap12Endpoint/"/>
</wsdl:port>
<wsdl:port name="HelloWorldWSHttpEndpoint" binding="ns:HelloWorldWSHttpBinding">
<http:address location="http://localhost:8080/WebProject_DB16/services/HelloWorldWS.HelloWorldWSHttpEndpoint/"/>
</wsdl:port>
<wsdl:port name="HelloWorldWSHttpsEndpoint" binding="ns:HelloWorldWSHttpBinding">
<http:address location="https://localhost:8443/WebProject_DB16/services/HelloWorldWS.HelloWorldWSHttpsEndpoint/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

And this my Android App:

public class WSClientActivity extends Activity {

    private TextView tv;
    private Button bt;
    private EditText et;

    private static final String SOAP_ACTION = "http://WS.androidroleplay.fk4.de.hs_bremen.de/GetSumOfTwoInts";
    private static final String METHOD_NAME = "GetSumOfTwoInts";
    private static final String NAMESPACE = "http://WS.androidroleplay.fk4.de.hs_bremen.de";
    private static final String NAMESPACE2 = "http://WS.androidroleplay.fk4.de.hs_bremen.de/xsd";
    private static final String URL = "http://192.168.178.28:8080/WebProject_DB16/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.setCategoryId(1);
        C.setDescription("Client Desc");
        C.setName("Client Name"); 

        /*
         * Set the category to be the argument of the web service method
         * 
         * */

        PropertyInfo objekt = new PropertyInfo();
        objekt.setName("C");
        objekt.setValue(C);
        objekt.setType(C.getClass());
        objekt.setNamespace(NAMESPACE2);

        Request.addProperty(objekt);

        /*
         * Set the web service envelope
         * 
         * */
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(Request);

        envelope.addMapping(NAMESPACE2, "Category",new Category().getClass());

//        envelope.avoidExceptionForUnknownProperty=true;

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        /*
         * Call the web service and retrieve result ... how luvly <3
         * 
         * */
        try
        {
            List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
            headerList.add(new HeaderProperty("Authorization", "Basic " + org.kobjects.base64.Base64.encode("tomcat:tomcat".getBytes()))); // "username:password"

            androidHttpTransport.call(SOAP_ACTION, envelope, headerList);
            SoapObject response = (SoapObject)envelope.getResponse();
            C.setCategoryId(Integer.parseInt(response.getProperty(0).toString()));
            C.setName(response.getProperty(1).toString());
            C.setDescription(response.getProperty(2).toString());

            TextView tv = (TextView)findViewById(R.id.texthallo);
            tv.setText("CategoryId: " +C.getCategoryId() + " Name: " + C.getName() + " Description: " + C.getDescription()); //"CategoryId: " +C.getCategoryId() + " Name: " + C.getName() + " Description: " + C.getDescription()
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

If i start the App, i get this Exception:

W/System.err(783): java.lang.RuntimeException: Unknown Property: categoryId

I could receive the complex object, if I comment this out:

PropertyInfo objekt = new PropertyInfo();
        objekt.setName("C");
        objekt.setValue(C);
        objekt.setType(C.getClass());
        objekt.setNamespace(NAMESPACE2);

        Request.addProperty(objekt);

and this:

envelope.addMapping(NAMESPACE2, "Category",new Category().getClass());

**

OR

**

i could send the complex object if i comment this out:

SoapObject response = (SoapObject)envelope.getResponse();
            C.setCategoryId(Integer.parseInt(response.getProperty(0).toString()));
            C.setName(response.getProperty(1).toString());
            C.setDescription(response.getProperty(2).toString());

But if i want both, i get the already named exception.

I think maybe the problem is, that i use two different Namespaces (NAMESPACE and NAMESPACE2), but if i only use NAMESPACE my webservice thows this excetion:

[ERROR] Exception occurred while trying to invoke service method GetSumOfTwoInts
org.apache.axis2.AxisFault: Unknow type {http://WS.androidroleplay.fk4.de.hs_bremen.de}Category
    at org.apache.axis2.databinding.utils.BeanUtil.deserialize(BeanUtil.java:349)

or if i only use NAMESPACE2 i get this exception:

[ERROR] Exception occurred while trying to invoke service method GetSumOfTwoInts
org.apache.axis2.AxisFault: namespace mismatch require http://WS.androidroleplay.fk4.de.hs_bremen.de found http://WS.androidroleplay.fk4.de.hs_bremen.de/xsd

EDIT:

It works if I want to send a complex object and and revieve an "normal" objekt like a String or int or if i send a normal object and recieve an complex object.

But if i want send AND revice an complex object i have trouble... :(


Solution

  • As I commented you before, the

    W/System.err(783): java.lang.RuntimeException: Unknown Property: categoryId
    

    exception is caused because your member variables in Category class must be in lowecase.

    And try this for send and receive complex objects:

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        Category C = new Category();
        C.setCategoryId(1);
        C.setDescription("Client Desc");
        C.setName("Client Name"); 
    
        Request.addProperty(C.getClass().getSimpleName(), C);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        envelope.addMapping(NAMESPACE2, C.getClass().getSimpleName(), C.getClass());
    
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        Category ans = null;
        try {
            androidHttpTransport.call(NAMESPACE + METHOD_NAME, envelope);
            ans = (Category)envelope.getResponse();
            tv.setText("CategoryId: " + ans.getCategoryId() + " Name: " + ans.getName() + " Description: " + ans.getDescription()); 
        }
        catch(Exception e) {
            e.printStackTrace();
        }