2011-12-15 26 views
3

我必須調用一個Web服務,其中通過kSoap2方法調用Web服務,現在在這一個節點中是一個數組,因此我如何傳遞它。在Ksoap2中使用Web服務傳遞數組

POST /opera/OperaWS.asmx HTTP/1.1 
Host: 182.71.19.26 
Content-Type: text/xml; charset=utf-8 
Content-Length: length 
SOAPAction: "http://tempuri.org/SendGroupMessageNotification" 

<?xml version="1.0" encoding="utf-8"?> 
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
    <SendGroupMessageNotification xmlns="http://tempuri.org/"> 
     <reciverMemberId> 
     <Group> 
      <groupid>string</groupid> 
      <groupMembers> 
      <Id>string</Id> 
      <Id>string</Id> 
      </groupMembers> 
     </Group> 
     <Group> 
      <groupid>string</groupid> 
      <groupMembers> 
      <Id>string</Id> 
      <Id>string</Id> 
      </groupMembers> 
     </Group> 
     </reciverMemberId> 
     <MemberId>int</MemberId> 
     <MESSAGE>string</MESSAGE> 
     <CREATEDDATE>string</CREATEDDATE> 
     <isUrgent>boolean</isUrgent> 
     <Predifnemessage>string</Predifnemessage> 
    </SendGroupMessageNotification> 
    </soap:Body> 
</soap:Envelope> 

所以在上面的Web服務,我怎麼能我設置的值對於其餘reciverMemberId

參數與的PropertyInfo輕鬆設置。

爲此,我做了一些代碼,如下

static class Group implements KvmSerializable 
    { 
     String groupid; 
     Vector groupMembers; 
     public Group(String groupId,Vector groupmembers) 
     { 
      this.groupid=groupId; 
      this.groupMembers=groupmembers; 
     } 

     public Object getProperty(int i) 
     { 
      switch(i) 
      { 
       case 0: 
        return groupid; 
       case 1: 
        return groupMembers; 
      } 
      return null; 
     } 

     public int getPropertyCount() 
     { 
      return 2; 
     } 

     public void setProperty(int i, Object o) 
     { 
      switch(i) 
      { 
       case 0: 
        groupid=o.toString(); break; 
       case 1: 
        groupMembers=(Vector) o; 
        break; 
      } 
     } 

     public void getPropertyInfo(int i, Hashtable hshtbl, PropertyInfo pi) 
     { 
      switch(i) 
      { 
       case 0: 
        pi.type=PropertyInfo.STRING_CLASS; 
        pi.name="groupid"; 
        break; 
       case 1: 
        pi.type=PropertyInfo.VECTOR_CLASS; 
        pi.name="groupMembers"; 
        break; 
      } 
     } 
    } 
    static class RereciverMemberId implements KvmSerializable 
    { 

     Group grp; 
     public RereciverMemberId() 
     { 
      Vector grpMembers=new Vector(); 
      grpMembers.add("29"); 
      grpMembers.add("36"); 
      grp=new Group("1", grpMembers); 
     } 
     public Object getProperty(int i) 
     { 
      return grp; 
     } 

     public int getPropertyCount() 
     { 
      return 0; 
     } 

     public void setProperty(int i, Object o) 
     { 
      this.grp=(Group) o; 
     } 

     public void getPropertyInfo(int i, Hashtable hshtbl, PropertyInfo pi) 
     { 
      pi.type=grp.getClass(); 
      pi.name="Group"; 
     } 

    } 
    public static void sendGroupMessageNotification() 
    { 
     SOAP_ACTION="http://tempuri.org/SendGroupMessageNotification"; 
     METHOD_NAME="SendGroupMessageNotification"; 
     SoapObject myObject = new SoapObject(NAMESPACE,METHOD_NAME); 

     //String str="<Group><groupid>1</groupid><groupMembers><Id>29</Id><Id>36</Id></groupMembers></Group>"; 
     RereciverMemberId rec=new RereciverMemberId(); 
     PropertyInfo pi = new PropertyInfo(); 
     pi.setName("reciverMemberId"); 
     pi.setValue(rec); 
     pi.setType(rec.getClass()); 
     myObject.addProperty(pi); 
     PropertyInfo p = new PropertyInfo(); 
     p.setName("MemberId"); 
     p.setValue(1); 
     p.setType(PropertyInfo.INTEGER_CLASS); 
     myObject.addProperty(p); 
     p = new PropertyInfo(); 
     p.setName("MESSAGE"); 
     p.setValue("Test Message From JAVA"); 
     p.setType(PropertyInfo.STRING_CLASS); 
     myObject.addProperty(p); 

     p = new PropertyInfo(); 
     p.setName("CREATEDDATE"); 
     p.setValue("15 Dec 2011"); 
     p.setType(PropertyInfo.STRING_CLASS); 
     myObject.addProperty(p); 

     p = new PropertyInfo(); 
     p.setName("isUrent"); 
     p.setValue(false); 
     p.setType(PropertyInfo.BOOLEAN_CLASS); 
     myObject.addProperty(p); 

     p = new PropertyInfo(); 
     p.setName("Predifnemessage"); 
     p.setValue("Hello"); 
     p.setType(PropertyInfo.STRING_CLASS); 
     myObject.addProperty(p); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(myObject); 
     HttpTransportSE transport = new HttpTransportSE(URL); 
     try 
     { 
      transport.call(SOAP_ACTION, envelope); 
     } 
     catch (IOException ex) 
     { 
      Logger.getLogger(SoapWebServices.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     catch (XmlPullParserException ex) 
     { 
      Logger.getLogger(SoapWebServices.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     try 
     { 
       SoapPrimitive result = (SoapPrimitive) envelope.getResponse(); 
       System.out.println(result.toString()); 
     } 
     catch (SoapFault ex) 
     { 
      Logger.getLogger(SoapWebServices.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
+0

你有沒有考慮使用JAX?您只需創建一個包含List(=您的數組)的類並相應地對其進行註釋。 – Fildor 2011-12-15 10:36:13

+0

任何人都可以幫我解決這個問題 – 2011-12-15 12:11:30

回答

7

這可以通過相應節點的不同SoapObject來完成,因此像上述問題,我們必須進行兩次肥皂對象之一集團等爲GroupMembers。

整個代碼

public static String sendGroupMessageNotification(ArrayList<String>   
groupIdList,ArrayList<String> members,String senderId,String messageText,boolean isUrgentFlag) 
    { 
     SOAP_ACTION = "http://tempuri.org/SendGroupMessageNotification"; 
     METHOD_NAME = "SendGroupMessageNotification"; 

     Calendar currentDate = Calendar.getInstance(); 
     SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss"); 
     String dateNow = formatter.format(currentDate.getTime()); 
     SoapObject myObject = new SoapObject(NAMESPACE, METHOD_NAME); 
     SoapObject groupSoap=new SoapObject(NAMESPACE,METHOD_NAME); 
     SoapObject groupMembers=new SoapObject(NAMESPACE,METHOD_NAME); 


     groupSoap.addProperty("groupid","1"); 
     groupMembers.addProperty("Id","29"); 
     groupMembers.addProperty("Id","36"); 
     groupSoap.addProperty("groupMembers",groupMembers); 


     PropertyInfo receiverMemberid = new PropertyInfo(); 
     receiverMemberid.setName("reciverMemberId"); 
     receiverMemberid.setValue(groupSoap); 
     receiverMemberid.setType(groupSoap.getClass()); 
     myObject.addProperty(receiverMemberid); 

     PropertyInfo memberId=new PropertyInfo(); 
     memberId.setName("MemberId"); 
     memberId.setValue(Integer.parseInt(senderId)); 
     memberId.setType(PropertyInfo.INTEGER_CLASS); 
     myObject.addProperty(memberId); 

     PropertyInfo message=new PropertyInfo(); 
     message.setName("MESSAGE"); 
     message.setValue(messageText); 
     message.setType(PropertyInfo.STRING_CLASS); 
     myObject.addProperty(message); 

     PropertyInfo createDate=new PropertyInfo(); 
     createDate.setName("CREATEDDATE"); 
     createDate.setValue(dateNow); 
     createDate.setType(PropertyInfo.STRING_CLASS); 
     myObject.addProperty(createDate); 

     PropertyInfo isUrgent=new PropertyInfo(); 
     isUrgent.setName("isUrent"); 
     isUrgent.setValue(isUrgentFlag); 
     isUrgent.setType(PropertyInfo.BOOLEAN_CLASS); 
     myObject.addProperty(isUrgent); 

     PropertyInfo predifMessage=new PropertyInfo(); 
     predifMessage.setName("Predifnemessage"); 
     predifMessage.setValue("Hello"); 
     predifMessage.setType(PropertyInfo.STRING_CLASS); 
     myObject.addProperty(predifMessage); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(myObject); 
     HttpTransportSE transport = new HttpTransportSE(URL); 
     try 
     { 
      transport.call(SOAP_ACTION, envelope); 
     } 
     catch (IOException ex) 
     { 
      Logger.getLogger(SoapWebServices.class.getName()).log(Level.SEVERE, null, ex); 
      return null; 
     } 
     catch (XmlPullParserException ex) 
     { 
      Logger.getLogger(SoapWebServices.class.getName()).log(Level.SEVERE, null, ex); 
      return null; 
     } 
     try 
     { 
      SoapPrimitive result = (SoapPrimitive) envelope.getResponse(); 
      return result.toString(); 
     } 
     catch (SoapFault ex) 
     { 
      Logger.getLogger(SoapWebServices.class.getName()).log(Level.SEVERE, null, ex); 
      return null; 
     } 
    } 

確定,如果任何人有問題,然後讓我知道

相關問題