关于java xmlrpc调用时的int参数遇到问题
-
本人打算在android手机上实现im_chat功能,
调其中一个方法时遇到问题,当方法中有参数是int类型时失败。如果参数为字符串类型,则没问题。
不知各位仁兄有没遇到过类似问题,请指教。
调用片段:
XmlRpcClient xmlrpc = new XmlRpcClient();
XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();
xmlrpcConfgDb.setEnabledForExceptions(true);
xmlrpcConfgDb.setServerURL(new URL(
"http://localhost:8069/xmlrpc/object"));
xmlrpc.setConfig(xmlrpcConfgDb);
Object[] params = new Object[] { "sentai", 1, "admin", "im_chat.session",
"session_get",new Integer(5)};
try {
Object[] o = (Object[]) xmlrpc.execute("execute_kw", params);
} catch (Exception e) {
e.printStackTrace();
}
出错信息:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.apache.xmlrpc.parser.XmlRpcResponseParser.addResult(XmlRpcResponseParser.java:61)
at org.apache.xmlrpc.parser.RecursiveTypeParserImpl.endValueTag(RecursiveTypeParserImpl.java:78)
at org.apache.xmlrpc.parser.XmlRpcResponseParser.endElement(XmlRpcResponseParser.java:186)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:186)
at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:156)
at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:143)
at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:69)
at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:56)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:167)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:137)
at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:126)
at odootest.imchat.main(imchat.java:158)
被调用的方法:
def session_get(self, cr, uid, user_to, context=None):
其中user_to为int型,在设用其他方法,当参数为字符串类型时均成功能。 -
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
java的问题。。不会 -
[quote author=lixuchao link=topic=17147.msg30551#msg30551 date=1434618005]
用python的xmlrpclib调用没有问题。
代码段如下:
model="im_chat.session" ##要运行哪个类中的函数?
method="session_get" ##要运行的函数
result = sock.execute(dbname, uid, pwd,
model,method,5 )
print "\n==== %s of %s ====" % (model, method)
pprint(result)
[/quote]
python不是严格类型绑定的语言,java是,所以你老老实实用String吧。 -
你的这个错误信息,我猜是RPC调用出错后,JAVA期待的是Integer类型错误返回值而实际的错误返回值是Sting Type引起的。
的确是传入参数错误,to_user应该是一个Array List而不是Integer, 理论上你的python代码也会返回错误 -
现在情况是python能成功调用,java调用失败,另外,我写了一个module做了测试,返回结果是一个整数,出错提示相同。
python调用成功时返回结果是:
==== im_chat.session of session_get ====
{'state': 'closed',
'users': [{'id': 1, 'im_status': 'online', 'name': 'Administrator'},
{'id': 5, 'im_status': 'offline', 'name': u'\u674e\u6829\u8d85'}],
'uuid': '438e8348-0009-46b5-a411-229b19fe4967'}
有可能是java的xmlrpc不支持此种类型结果吗?
另外,把参数改成ArrayLlist后出错如下:
java.lang.ClassCastException: java.lang.String cannot be cast to [Ljava.lang.Object;
at odootest.imchat.main(imchat.java:161) -
问题已解决。
调用时用 HashMap a = (HashMap) xmlrpc.execute("execute", params); "execute"改为"execute_kw"就有问题,不知两个有什么区别。
就可以。
程序段如下:
try{
XmlRpcClient xmlrpc = new XmlRpcClient();
XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();
xmlrpcConfgDb.setEnabledForExceptions(true);
xmlrpcConfgDb.setServerURL(new URL(
"http://localhost:8069/xmlrpc/object"));
xmlrpc.setConfig(xmlrpcConfgDb);
Object[] params = new Object[] { "sentai",1, "admin", "im_chat.session",
"session_get",5};
try {
HashMap a = (HashMap) xmlrpc.execute("execute", params);
System.out.println(a);
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}