Java 如何在Android应用程序开发中处理WSDL complexType?
我正在使用EclipseIndigo创建一个Android应用程序来调用Web服务。这已经在一个Java项目中工作,但我知道这是一个完全不同的事情 我知道您可以使用ksoap2库并像这样调用Web服务:Java 如何在Android应用程序开发中处理WSDL complexType?,java,android,web-services,complextype,Java,Android,Web Services,Complextype,我正在使用EclipseIndigo创建一个Android应用程序来调用Web服务。这已经在一个Java项目中工作,但我知道这是一个完全不同的事情 我知道您可以使用ksoap2库并像这样调用Web服务: SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); request.addProperty("something","Somevalue"); 但我不知道当您的WSDL为complexType时如何调用webservice,
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("something","Somevalue");
但我不知道当您的WSDL为complexType时如何调用webservice,如下所示:
<?xml version='1.0' encoding='UTF-8'?>
<definitions name='Gator' targetNamespace='http://home.com/gatorws/ws/Gator/' xmlns='http://schemas.xmlsoap.org/wsdl/' xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' xmlns:tns='http://home.com/gatorws/ws/Gator/' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<types>
<xs:schema targetNamespace='http://home.com/gatorws/ws/Gator/' version='1.0' xmlns:tns='http://home.com/gatorws/ws/Gator/' xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='Gator'>
<xs:complexType>
<xs:sequence>
<xs:element name='idBus' type='tns:IdBusCT'/>
<xs:element name='infoGPS' type='tns:InfoGPSCT'/>
<xs:element name='infoBus' type='tns:InfoBusCT'/>
<xs:element name='infoMore' type='tns:InfoMoreCT'/>
<xs:element maxOccurs='unbounded' minOccurs='0' name='msjcs' nillable='true' type='tns:Mesaje'/>
<xs:element name='msjOPT' type='tns:MsjCor'/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='GatorFault'>
<xs:complexType>
<xs:sequence>
<xs:element name='result' type='xs:int'/>
<xs:element name='msj' type='xs:string'/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name='GatorResponse'>
<xs:complexType>
<xs:sequence>
<xs:element name='result' type='tns:Result'/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name='IdBusCT'>
<xs:sequence>
<xs:element name='idOp' type='xs:int'/>
<xs:element name='codBus' nillable='true' type='xs:string'/>
<xs:element name='codEqu' nillable='true' type='xs:string'/>
<xs:element name='codType' type='xs:int'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='InfoGPSCT'>
<xs:sequence>
<xs:element name='lat' type='xs:double'/>
<xs:element name='lng' type='xs:double'/>
<xs:element name='date' type='xs:string'/>
<xs:element name='direction' type='xs:int'/>
<xs:element name='valid' type='xs:int'/>
<xs:element name='velocity' type='xs:double'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='InfoBusCT'>
<xs:sequence>
<xs:element default='1' name='ignition' nillable='true' type='xs:int'/>
<xs:element default='0' name='emergency' nillable='true' type='xs:int'/>
<xs:element default='0' name='idRoute' nillable='true' type='xs:int'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='MsjCor'>
<xs:sequence>
<xs:element name='last' type='xs:lng'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='InfoMoreCT'>
<xs:sequence>
<xs:element default='' name='CODDriver' nillable='true' type='xs:string'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='Result'>
<xs:sequence>
<xs:element name='result' type='xs:int'/>
<xs:element maxOccurs='unbounded' minOccurs='0' name='msjOPT' nillable='true' type='tns:Mesaje'/>
<xs:element name='msjcs' nillable='true' type='tns:MsjCor'/>
</xs:sequence>
</xs:complexType>
<xs:complexType name='Mesaje'>
<xs:sequence>
<xs:element name='type' type='xs:int'/>
<xs:element name='text' type='xs:string'/>
<xs:element name='date' type='xs:string'/>
<xs:element name='corr' type='xs:int'/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</types>
<message name='Gator_Gator'>
<part element='tns:Gator' name='Gator'></part>
</message>
<message name='Gator_GatorResponse'>
<part element='tns:GatorResponse' name='GatorResponse'></part>
</message>
<message name='GatorFault'>
<part element='tns:GatorFault' name='GatorFault'></part>
</message>
<portType name='Gator'>
<operation name='Gator' parameterOrder='Gator'>
<input message='tns:Gator_Gator'></input>
<output message='tns:Gator_GatorResponse'></output>
<fault message='tns:GatorFault' name='GatorFault'></fault>
</operation>
</portType>
<binding name='GatorBinding' type='tns:Gator'>
<soap:binding style='document' transport='http://schemas.xmlsoap.org/soap/http'/>
<operation name='Gator'>
<soap:operation soapAction='http://home.com/gatorws/ws/Gator/Gator'/>
<input>
<soap:body use='literal'/>
</input>
<output>
<soap:body use='literal'/>
</output>
<fault name='GatorFault'>
<soap:fault name='GatorFault' use='literal'/>
</fault>
</operation>
</binding>
<service name='Gator'>
<port binding='tns:GatorBinding' name='GatorImplPort'>
<soap:address location='https://10.0.2.2:8080/gatoryws/Gator?wsdl'/>
</port>
</service>
</definitions>
我只是能够从网络服务得到一个失败的答案
我试过了,但那是针对.NET Web服务的
我也试过,但我不明白在我的情况下它是如何工作的
还有其他我可以使用的工具吗?
我应该迁移到Android Studio吗?我发现这非常有用: 这里我将告诉您如何检索复杂类型对象。我为此创建了ComplexTypeActivity。对于UI,我刚刚添加了一个按钮和一个文本视图来显示结果。让我们创建复杂类型类。 在这里,我创建了一个包含3个字符串值的类。我把它命名为SimpleObject
public class SimpleObject implements KvmSerializable {
private String name;
private String address;
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public Object getProperty(int pid) {
// TODO Auto-generated method stub
switch (pid) {
case 0:
return this.id;
case 1:
return this.name;
case 2:
return this.address;
default:
break;
}
return null;
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 3;
}
@Override
public void getPropertyInfo(int index, Hashtable htable, PropertyInfo info) {
// TODO Auto-generated method stub
switch (index) {
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "id";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "name";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "address";
break;
}
}
@Override
public void setProperty(int index, Object value) {
// TODO Auto-generated method stub
switch (index) {
case 0:
this.id = value.toString();
break;
case 1:
this.name = value.toString();
break;
case 2:
this.address = value.toString();
break;
}
}
}
注意,我在这里实现了KvmSerializable接口。使用该接口KSOAP将这些类属性映射到soap消息
和前面一样,我将在按下按钮时调用web服务
btnShowDetails.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LoadDetailsTask task = new LoadDetailsTask();
task.execute("ID01");
}
});
class LoadDetailsTask extends AsyncTask<string simpleobject="" void=""> {
private final String NAMESPACE = "http://service.blog.anjula.com/";
private final String URL = "http://112.135.137.22:8080/BlogWebService/BlogWebService?WSDL";
private final String SOAP_ACTION = "http://service.blog.anjula.com/getDetails";
private final String METHOD_NAME = "getDetails";
SimpleObject result = new SimpleObject();
@Override
protected SimpleObject doInBackground(String... addId) {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo idListPropety = new PropertyInfo();
idListPropety.setName("id");
idListPropety.setValue(addId[0]);
idListPropety.setType(addId[0].getClass());
request.addProperty(idListPropety);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "SimpleObject",
new SimpleObject().getClass());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();
result.setId(response.getProperty("id").toString());
result.setName(response.getProperty("name").toString());
result.setAddress(response.getProperty("address").toString());
return result;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(SimpleObject result) {
// TODO Auto-generated method stub
if (result != null)
textViewResult.setText("Id: " + result.getId() + "\nName: "
+ result.getName() + "\nAddress: "
+ result.getAddress());
}
}
在URL中,我在那里输入了我的IP地址,因为我在我的计算机上运行了该服务。因此,如果您正在计算机中运行服务器应用程序,则必须将该IP地址更改为您的IP地址。
按下按钮后,程序将调用此方法
public SimpleObject getDetails(String id){
SimpleObject result = new SimpleObject();
result.setId(id);
result.setName("Anjula");
result.setAddress("Kaduwela");
return result;
}
仅此而已:D如果您想知道Android Studio是否能完全成功地帮助您的web请求,您应该阅读您尝试使用的协议。
public SimpleObject getDetails(String id){
SimpleObject result = new SimpleObject();
result.setId(id);
result.setName("Anjula");
result.setAddress("Kaduwela");
return result;
}