Reputation: 181
I am writing a java program where I want to simulate a client-server relationship with remote procedure calls using xmlrpc.
However when I try to remotely call the method I get this error message:
'JavaClient: XML-RPC Consumer Fault #java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer'
This is the method on the client side that is producing the error:
public String getHashsize() {
// Execute the remote call, using the handler
try
{
argHashsize = new Vector(); //see next method for comments
argHashsize.addElement(serverURL);
// make the call
String callit = ("GetSize.sendHashSize");
sizeHash = (Integer)client.execute(callit, argHashsize );
}
// Use XmlRpcException errors
catch (XmlRpcException exception) {
System.err.println("JavaClient: XML-RPC Consumer Fault #" +
Integer.toString(exception.code) + ": " +
exception.getCause() + "" + exception.toString());
} catch (Exception exception) {
System.err.println("JavaClient: XML-RPC Consumer Fault #" + exception.toString());
}
String StrsizeHash = Integer.toString(sizeHash); // Cast sizeHash to String
return StrsizeHash;
}
This is the method that I am attempting to call:
public String sendHashSize(String getCaller) {
// send back info to Message Broker
sendbackserverdata = "number of staff: " + theHashtable.noStaff()
+ " Caller: " + getCaller
+ " Port: " + getServerPort()
+ " " + getSendtime();
//Store server info in Hashtable persist - could be a database
/*persist.put("" + theHashtable.noStaff(),"" + sendbackserverdata);
sendServerData = sendbackserverdata;
stats.StoreinStatsHashtable("" + theHashtable.noStaff(), sendbackserverdata);*/
return sendbackserverdata; // return to Consumer
}
Upvotes: 0
Views: 1280
Reputation: 181
The sendHashSize() method should have the following code instead:
public int sendHashSize(String getCaller)
{
// return to Consumer
return theHashtable.noStaff();
}
Upvotes: 0
Reputation: 53535
Instead of:
sizeHash = (Integer)client.execute(callit, argHashsize );
try:
sizeHash = Integer.valueOf(client.execute(callit, argHashsize ));
Of course that you have to make sure that a real integer is sent otherwise you'll get an exception for trying to convert a non-integer string to an integer (NumberFormatException).
Preferred approach:
The method sendHashSize()
should return an int/Integer - and then you don't have to cast to Integer or use Integer.valueOf()
Upvotes: 1
Reputation: 1502396
Your sendHashSize
method returns a String
. It's not even the string representation of an integer - it's a string starting with "number of staff:".
I'm not surprised that it failed with an exception - it's just surprising that you expected it to work. Which integer would you have expected as a result?
Either change sendHashSize
to return an int
/Integer
(perhaps theHashtable.noStaff()
?), or avoid trying to cast it to an Integer
on the calling side.
Additionally, this code is misleading:
catch (Exception exception) {
System.err.println("JavaClient: XML-RPC Consumer Fault #" + exception.toString());
}
It's not an "XML-RPC Consumer Fault" - you've already caught those in the previous line. You've got an exception because you're trying to cast a String
to an Integer
... which is nothing to do with the XML-RPC layer.
Upvotes: 5