Java – Pass Array using Web services in Ksoap2

javaksoap2web services

I have to call a web service in which web service is called by kSoap2 method, now in this one node is a Array so how i can pass it.

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>

So in the above web service, how i can i set the values for the reciverMemberId

remaining parameters is set easily with the propertyInfo.

For this i made some code as below

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);
        }
    }

Best Answer

This can be done by making different SoapObject of corresponding Node, so like in the above problem we have to make two soap objects one for Group, and other for GroupMembers.

entire code

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;
        }
    } 

Ok if anyone have problem then let me know