带有Android的KSOAP2抛出“未知属性:”异常。我只能接收或发送复杂的对象

带有Android的KSOAP2抛出“未知属性:”异常。我只能接收或发送复杂的对象,android,exception,object,ksoap2,Android,Exception,Object,Ksoap2,我正试图修改复杂对象并将其发送到我的Web服务,但我的问题是,我只能接收或发送,没有任何例外 代码基于此示例 这是我在Tomcat7上运行的Java Web服务: public class HelloWorldWS { public Category GetSumOfTwoInts(Category C ) { Category back= new Category(); back.setCategoryId(112); ba

我正试图修改复杂对象并将其发送到我的Web服务,但我的问题是,我只能接收或发送,没有任何例外

代码基于此示例

这是我在Tomcat7上运行的Java Web服务:

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;
    }
}
这是生成的WSDL文件:

<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>
我可以接收复杂对象,如果我将其注释掉:

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

        Request.addProperty(objekt);
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());
这是:

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

**

如果我对此进行注释,我可以发送复杂对象:

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

        Request.addProperty(objekt);
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());
但是如果我想要两者,我会得到已经命名的异常

我认为问题可能在于,我使用了两个不同的名称空间NAMESPACE和NAMESPACE2,但如果我只使用名称空间,我的Web服务将考虑以下例外情况:

[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)
或者,如果我只使用名称空间2,则会出现以下异常:

[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
编辑:

如果我想发送一个复杂对象并修改一个普通对象,比如字符串或int,或者如果我发送一个普通对象并接收一个复杂对象,那么它是有效的


但如果我想发送和修改一个复杂的对象,我有麻烦…:

正如我之前评论的那样

W/System.err(783): java.lang.RuntimeException: Unknown Property: categoryId
导致异常的原因是类别类中的成员变量必须为小写

对于发送和接收复杂对象,请尝试以下操作:

    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();
    }
就我而言 SoapSerializationEnvelope类包含方法ReadSerializableMPLULParser解析器KvmSerializable obj,中存在以下条件

。。 name.equalsinfo.name&&parser.getNamespace.equalsinfo.namespace

所以我检查了我的数据类型的类,发现getPropertyInfo方法将不正确的名称空间放入属性中


因此属性的名称空间必须与解析器的名称空间相同。

我有一个基于Java的Web服务,所以我认为我不必使用这个信封。dotNet=true;还是不?我不确定,但我大部分时间都看到了。