如何在Android应用程序开发中处理WSDL complexType?

时间:2016-02-17 15:40:26

标签: java android web-services complextype

我正在使用Eclipse Indigo创建一个Android应用程序来调用Web服务。这已经在Java项目上工作,但我知道这是一个完全不同的事情。

我知道你可以使用kso​​ap2库并调用这样的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>

我只能从网络服务中获得失败的答案。

我尝试了http://www.wsdl2code.com,但这是针对.NET webservices。

我也试过http://easywsdl.com/,但我不明白它是如何运作的。

我可以使用其他工具吗? 我应该迁移到Android Studio吗?

1 个答案:

答案 0 :(得分:0)

我发现了这个,它真的很有帮助: http://binarylifebyanjula.blogspot.cl/2013/11/android-webservices-ksoap2-complex.html

这里我将告诉你如何检索复杂类型的对象。我为此创建了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());
  }

 }

在这里你可以看到我正在使用字段名称来映射输出。

result.setId(response.getProperty("id").toString());
    result.setName(response.getProperty("name").toString());
    result.setAddress(response.getProperty("address").toString());

在网址中我输入了我的IP地址,因为我在计算机上运行了该服务。因此,如果您在计算机中运行服务器应用程序,则必须将该IP地址更改为您的IP地址。 按下按钮后,程序将调用此方法。

public SimpleObject getDetails(String id){
        SimpleObject result = new SimpleObject();        
        result.setId(id);
        result.setName("Anjula");
        result.setAddress("Kaduwela");        
        return result;        
    }

这就是全部:D