问题
I am trying to use https://code.google.com/p/android-json-rpc/ for android and use https://github.com/RitwikSaikia/jsonrpc on the java server.
I have followed the examples for android-json-rpc client and jsonrpc server setup.
When using android-json-rpc in my Android app I do the following.
JSONRPCClient client = JSONRPCClient.create("http://service/uri",JSONRPCParams.Versions.VERSION_2);
client.setConnectionTimeout(2000);
client.setSoTimeout(2000);
try
{
client.callString("SimpleCalculatorImpl");
double i = client.callDouble("add", 56, 25);
}
catch (JSONRPCException e)
{
e.printStackTrace();
}
For the Java server I have followed the sample at https://github.com/RitwikSaikia/jsonrpc#defining-interfaces and at https://github.com/RitwikSaikia/jsonrpc#hosting-the-service
When I call the method
String string = client.callString("SimpleCalculatorImpl");
It throws a JSONRPCException: Cannot convert result to String which comes from inside the callString method.
Has anyone encountered this error before or know of better a library to use for an Android client to a Java server for JSON?
回答1:
i haven't tried the library but i know that this exception happens when the json is not parsed correctly, try to copy the result and run it through a json validator like JSONLint
hope this helps.
回答2:
I actually just used https://github.com/FasterXML/jackson on both Android client side and Java server side and created my own servlet with reflection to handle making calls to methods on the Java server and getting back the results on the Android client
回答3:
I haven't used either of the libraries, but from looking at it briefly, it seems like the jsonrpc server library registers a prefix handler with executor.addHandler("calc", calcImpl, Calculator.class);
, so the fully qualified name of the "add" method would be "calc.add". So just try following in your code:
double i = client.callDouble("calc.add", 56, 25);
No need to call client.callString("SimpleCalculatorImpl");
, as callString() should be used for RPC calls that return String result.
来源:https://stackoverflow.com/questions/15957436/how-to-use-android-json-rpc-for-androidclient-javaserver-setup