私は自分の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>
そして、これは私の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
おそらく問題は、2 つの異なる名前空間 (と) を使用していることだと思いますが、 Web サービス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
編集:
String
複雑なオブジェクトを送信し、またはまたは通常のオブジェクトを送信して複雑なオブジェクトを受信した場合のような「通常の」オブジェクトを再表示する場合に機能しint
ます。
しかし、複雑なオブジェクトを送信して修正したい場合は、問題があります... :(