我正在尝试修改和发送复杂的对象到我的网络服务/从我的网络服务,但我的问题是,我只能接收或发送,没有任何例外。
代码基于本教程的示例
这是我在 Tomcat7 上运行的 Java Webservice:
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());
**
或者
**
如果我将其注释掉,我可以发送复杂对象:
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
我的网络服务,就会出现这个异常:
[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
编辑:
如果我想发送一个复杂的对象并接收一个像 or 这样的“正常”对象,String
或者int
如果我发送一个正常的对象并接收一个复杂的对象,它就可以工作。
但是如果我想发送和修改一个复杂的对象,我会遇到麻烦...... :(