使用Ksoap2中的Web服务传递数组 [英] Pass Array using Web services in Ksoap2

查看:88
本文介绍了使用Ksoap2中的Web服务传递数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须调用一个Web服务,其中通过kSoap2方法调用Web服务,现在在这个节点中是一个数组,所以我可以传递它。

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>

所以在上面的网络服务中,我如何设置reciverMemberId的值

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

剩下的参数可以通过propertyInfo轻松设置。

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


推荐答案

这可以通过制作相应节点的不同SoapObject来完成,所以像上面的问题一样,我们必须为Group制作一个soap对象,为GroupMembers制作另一个。

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.

整个代码

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

这篇关于使用Ksoap2中的Web服务传递数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆