在 Ksoap2 中使用 Web 服务传递数组

2023-12-02

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

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的值

其余参数可以使用 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);
        }
    }

这可以通过为相应的 Node 创建不同的 SoapObject 来完成,所以像上面的问题一样,我们必须创建两个肥皂对象,一个用于 Group,另一个用于 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;
        }
    } 

好的,如果有人有问题请告诉我

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Ksoap2 中使用 Web 服务传递数组 的相关文章

  • 如何使用Spring WebClient进行同步调用?

    Spring Framework in 休息模板 https docs spring io spring framework docs current javadoc api org springframework web client R
  • 使用 JDBC 获取 Oracle 11g 的最后插入 ID

    我是使用 Oracle 的新手 所以我将放弃之前已经回答过的内容这个问题 https stackoverflow com questions 3131064 get id of last inserted record in oracle
  • Android 中的列表(特别是 RecyclerView 和 CardView)如何工作

    请原谅我问这个问题 但我是 Android 开发新手 尽管我正在尝试了解developer android com 网站上的基础知识 但大多数示例 即使他们说它们是为 Android Studio 构建的 尚未设置为使用 Gradle 因此
  • Oracle Java 教程 - 回答问题时可能出现错误

    我是 Java 新手 正在阅读 Oracle 教程 每个部分之后都有问题和答案 我不明白一个答案中的一句话 见下面的粗体线 来源是https docs oracle com javase tutorial java javaOO QandE
  • 如何在 Openfire 中使用 smack

    你好 我计划开发一个可以连接到 gtalk facebook 等的聊天客户端 我决定将 smack API 与 openfire 一起使用 但我需要很少的指导来了解如何将它与 openfire 服务器一起使用 openfire 是否提供了基
  • 文本在指定长度后分割,但不要使用 grails 打断单词

    我有一个长字符串 需要将其解析为长度不超过 50 个字符的字符串数组 对我来说 棘手的部分是确保正则表达式找到 50 个字符之前的最后一个空格 以便在字符串之间进行彻底的分隔 因为我不希望单词被切断 public List
  • 从 MS Access 中提取 OLE 对象(Word 文档)

    我有一个 Microsoft Access 数据库 其中包含一个包含 Microsoft Word 文档的 OLE 对象字段 我试图找到代码来检索保存在 OLE 对象中的文件 以便用户可以从我的 JavaFx 应用程序中的按钮下载它 但没有
  • 如何检测图像是否像素化

    之前有人在 SO 上提出过这样的问题 在Python中检测像素化图像 https stackoverflow com questions 12942365 detecting a pixelated image in python还有关于q
  • 是否可以从 servlet 内部以编程方式设置请求上下文路径?

    这是一个特殊情况 我陷入了处理 企业 网络应用程序的困境 企业应用程序正在调用request getContext 并将其与另一个字符串进行比较 我发现我可以使用 getServletContext getContextPath 获取 se
  • 如何使用正则表达式验证 1-99 范围?

    我需要验证一些用户输入 以确保输入的数字在 1 99 范围内 含 这些必须是整数 Integer 值 允许前面加 0 但可选 有效值 1 01 10 99 09 无效值 0 007 100 10 5 010 到目前为止 我已经制定了以下正则
  • 用于缓存的 Servlet 过滤器

    我正在创建一个用于缓存的 servlet 过滤器 这个想法是将响应主体缓存到memcached 响应正文由以下方式生成 结果是一个字符串 response getWriter print result 我的问题是 由于响应正文将不加修改地放
  • 如何从日期中删除毫秒、秒、分钟和小时[重复]

    这个问题在这里已经有答案了 我遇到了一个问题 我想比较两个日期 然而 我只想比较年 月 日 这就是我能想到的 private Date trim Date date Calendar calendar Calendar getInstanc
  • Play.application() 的替代方案是什么

    我是 Play 框架的新手 我想读取conf文件夹中的一个文件 所以我用了Play application classloader getResources Data json nextElement getFile 但我知道 play P
  • Java - 从 XML 文件读取注释

    我必须从 XML 文件中提取注释 我找不到使用 JDOM 或其他东西来让它们使用的方法 目前我使用 Regex 和 FileReader 但我不认为这是正确的方法 您可以使用 JDOM 之类的东西从 XML 文件中获取注释吗 或者它仅限于元
  • Lombok @Builder 不创建不可变对象?

    在很多网站上 我看到 lombok Builder 可以用来创建不可变的对象 https www baeldung com lombok builder singular https www baeldung com lombok buil
  • 如何处理 StaleElementReferenceException

    我正在为鼠标悬停工作 我想通过使用 for 循环单击每个链接来测试所有链接的工作条件 在我的程序中 迭代进行一次 而对于下一次迭代 它不起作用并显示 StaleElementReferenceException 如果需要 请修改代码 pub
  • Hadoop NoSuchMethodError apache.commons.cli

    我在用着hadoop 2 7 2我用 IntelliJ 做了一个 MapReduce 工作 在我的工作中 我正在使用apache commons cli 1 3 1我把库放在罐子里 当我在 Hadoop 集群上使用 MapReduceJob
  • 检查应用程序是否在 Android Market 上可用

    给定 Android 应用程序 ID 包名称 如何以编程方式检查该应用程序是否在 Android Market 上可用 例如 com rovio angrybirds 可用 而 com random app ibuilt 不可用 我计划从
  • 将对象从手机共享到 Android Wear

    我创建了一个应用程序 在此应用程序中 您拥有包含 2 个字符串 姓名和年龄 和一个位图 头像 的对象 所有内容都保存到 sqlite 数据库中 现在我希望可以在我的智能手表上访问这些对象 所以我想实现的是你可以去启动 启动应用程序并向左和向
  • 如何使用通配符模拟泛型方法的行为

    我正在使用 EasyMock 3 2 我想基于 Spring Security 为我的部分安全系统编写一个测试 我想嘲笑Authentication http docs spring io autorepo docs spring secu

随机推荐