4

I have a Python XML-RPC server that has a function that returns a tuple, e.g:

def function_name(first_parameter, second_parameter)
    #do something
    return (x,y)

I'm calling this function from Java in an Android application using aXMLRPC, the code is:

Object id = client.call("function_name", first_parameter, second_parameter);

How can I extract the two return values of the function from the Object id?

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
ste
  • 3,087
  • 5
  • 38
  • 73

3 Answers3

1

I believe if you use this library you can cast you Object id into a Tuple and then use methods inside to parse

Ahmad Dwaik 'Warlock'
  • 5,953
  • 5
  • 34
  • 56
1

The documentation for the XML-RPC library you're using to talk to the Python service says that there are only a limited number of possible return types; I don't know for sure without trying it, but clearly your Tuple is going to come back either as an array or perhaps as a Map<String, Object>. You're going to have to experiment a little but it shouldn't be too bad. Try something like this:

Object id = client.call("function_name", first_parameter, second_parameter);
System.out.println(id.getClass().getName());

to find out what you're dealing with, and then once you know, you can write the real code accordingly. Most likely, id will turn out to be an Object[2] containing the two values.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
0

What you want to achieve is unpacking values in the result Object. Which you can do writing a method that returns a Object[]:

public static Object[] unpack(Object array) 
{
    Object[] array2 = new Object[Array.getLength(array)];

    for(int i=0;i<array2.length;i++)
        array2[i] = Array.get(array, i);

    return array2;
}

This thread seems relevant.

Community
  • 1
  • 1
kiriloff
  • 25,609
  • 37
  • 148
  • 229