views:

13

answers:

1

I am using Apache java XML-RPC latest version.

The code for sending the array in Server is the following:

LinkedList<String> messages = new LinkedList<String>();

public String[] getMessages() {
    System.out.println("Sent messages");
    return messages.toArray(new String[messages.size()]);
}

To receive in the client I have tried something like this:

String[] result = (String[]) client.execute("Message.getMessages", new Object[] {});

This should cast the Object that I receive to the right type (String[]). Unfortunately it doesn't and I get the following error: [Ljava.lang.Object; cannot be cast to [Ljava.lang.String;

So I am wondering if anyone knows how to properly send and receive arrays in java XML-RPC?

A: 

Based on http://ws.apache.org/xmlrpc/types.html the client API will always return Object[] even if the server returns String[]

I would suggest looping over the result and calling toString() on each member. ugly, but working.

Bivas
I changed my code in the client, to cast to Object[] instead of String[]. Object[] result = (Object[]) client.execute("Message.getMessages", new Object[] {}); The thing is that this array has a length of 0 even though there are supposed to be items in it.
dominos
that's what you return from the server... unless you didn't include additional code that adds values to the list.
Bivas
Yea, I add values to the list before I return.
dominos
have you validated the server returning those messages?
Bivas
Yea, for some reason the server was not sending back the items in the array even though they existed on server and could be accessed using getFirst(). I rewrote some code and it looks like it is returning properly now. Casting toString on each Object separately also seems to work. So it looks like it is working now... thanks for your help.
dominos
would love if you could vote up and approve my answer :-)
Bivas
I would love to but it says I need 15 reputation to do that : \
dominos