我正在调用一项服务,该服务将我作为java.util.Collection
. 现在我的问题是如何从该对象中检索数据?如果我用 Java 打印它,我会得到类似[[Ljava.lang.Object;@7ff4d7c0]
. 这很正常,因为我只是在打印对象。
该服务来自 ofbiz 项目,并且运行正常。当我使用来自 ofbiz 的 Web 服务测试器时,我得到的返回值为:
{{party=[GenericEntity:Party][partyId,10045(java.lang.String)][partyTypeId,PERSON(java.lang.String)]}, {party=[GenericEntity:Party][partyId,10119(java.lang.String)][partyTypeId,PERSON(java.lang.String)]}}`
我如何在 Java 中实现这一点?
这是我的代码:
public static void main(String[] args) throws MalformedURLException, XmlRpcException {
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://127.0.0.1:8080/webtools/control/xmlrpc"));
config.setEnabledForExceptions(true);
config.setEnabledForExtensions(true);
XmlRpcClient client = new XmlRpcClient();
client.setConfig(config);
Map paramMap = new HashMap();
Map map1 = new HashMap();
//Verplichte parameters
paramMap.put("login.username", "admin");
paramMap.put("login.password", "opentaps");
paramMap.put("userLoginId", "David");
Object[] params = new Object[]{paramMap};
Map result = (Map) client.execute("getPartyFromUserLogin", params);
//System.out.println(result.values());
Collection parties = null;
parties = (Collection) result.values();
System.out.println("Got parties: " + parties.size());
System.out.println(parties.toString());
Iterator it = parties.iterator();
while(it.hasNext()){
Party object = (Party)it.next();
System.out.println("ok : "+ object.getPartyId());
}
}
好的,现在我正在尝试对其进行迭代并将其转换为派对对象(import org.opentaps.base.entities.Party;
)。猜猜它只是投错了班级?
错误日志:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to org.opentaps.base.entities.Party
at test.xmlrpcGetPartyEmail.main(xmlrpcGetPartyEmail.java:56)
使用此代码后的错误日志:
Iterator it = parties.iterator();
while(it.hasNext()){
System.out.println("ok : "+ it.next().getClass());
}
class java.util.HashMap$Values
Got parties: 1
[[Ljava.lang.Object;@2380bfe1]
ok : class [Ljava.lang.Object;
服务器日志:
ava:777:WARN ] Running the getPartyFromUserLogin Service...
[java] 2012-03-27 14:47:28,861 (http-0.0.0.0-8080-1) [ PartyServices.
ava:792:INFO ] PartyFromUserLogin number found: 2
[java] 2012-03-27 14:47:28,865 (http-0.0.0.0-8080-1) [ RequestHandler.
ava:641:INFO ] Ran Event [xmlrpc:#] from [request], result is [null]
[java] 2012-03-27 14:47:28,866 (http-0.0.0.0-8080-1) [ ControlServlet.
ava:328:INFO ] [[[xmlrpc] Request Done- total:0.056,since last([xmlrpc] Request
...):0.056]]