gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:32:29 25 4
gpt4 key购买 nike

我必须调用一个 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);
}
}

最佳答案

这可以通过为相应的节点制作不同的 SoapObject 来完成,所以就像在上面的问题中我们必须制作两个 soap 对象,一个用于 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;
}
}

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

关于java - 在 Ksoap2 中使用 Web 服务传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8518651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com