我正在调用一个服务,它将我作为java.util.Collection
的输出参数返回。现在我的问题是如何从该对象中检索数据?如果我用Java打印它,我会得到像[[Ljava.lang.Object;@7ff4d7c0]
这样的东西。这是正常的,因为我只是打印对象。
此服务来自ofbiz项目,并且工作正常。当我使用frombiz的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());
}
}
好的,现在我正在尝试迭代它并将其转换为Party对象(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]]
答案 0 :(得分:1)
您可以尝试使用
运行程序吗?Iterator it = parties.iterator();
while(it.hasNext()){
System.out.println("ok : "+ it.next().getClass());
}
并发布结果?