我试图修复并发送复杂的对象到/从我的web服务,但我的问题是,我可以只做recive或没有任何例外发送。带Android的KSoap 2引发“未知属性:”异常。我只能接收或发送复杂的对象
的代码是基于这个tutorial
的例子这是我的Java web服务上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;
}
}
这是生成的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>
这我的Android应用程序:
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();
}
}
}
如果我启动应用程序,我得到这个异常:
W/System.err(783): java.lang.RuntimeException: Unknown Property: categoryId
我无法接受复杂的对象,如果我评论了这一点:
PropertyInfo objekt = new PropertyInfo();
objekt.setName("C");
objekt.setValue(C);
objekt.setType(C.getClass());
objekt.setNamespace(NAMESPACE2);
Request.addProperty(objekt);
这:
envelope.addMapping(NAMESPACE2, "Category",new Category().getClass());
**
OR
**
我可以把复杂的对象,如果我评论了这一点:
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
),但如果我只用NAMESPACE
我的web thows这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)
,或者如果我只用NAMESPACE2我得到此异常:
[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
编辑:
它的工作原理,如果我想发出一个复杂的对象,并revieve一n“正常”的对象,如String
或int
或者如果我发送一个正常的对象并接收一个复杂的对象。
但是如果我想发送和revice一个复杂的对象,我有麻烦... :(
我有一个基于Java的Web服务,所以我想我不必使用这个'envelope.dotNet = true;'或不是? –
我不确定,但大部分时间我都看过。 –
它工作!!!!!非常感谢你,我很高兴:-)。 –