Hi,
AFAIK, you are right, option A is the way how it works (see:
http://ws.apache.org/xmlrpc/faq.html#arrays)
My only advice, make small tooling, eg.
public static List decodeList(Object element) {
if (element == null) {
return null;
}
if (element instanceof List) {
return (List) element;
}
if (element.getClass().isArray()) {
int length = Array.getLength(element);
LinkedList result = new LinkedList();
for (int i = 0; i < length; i++) {
result.add (Array.get(element, i));
}
return result;
}
return null;
}
With such method you can have option B.
Best regards
Stano
On Wed, Jul 8, 2009 at 23:37, Ken Tanaka <[email protected]> wrote:
> I'm using an xmlrpc-client 3.1.2 application to talk to an xmlrpc-server
> 3.1.2 server and want to pass an array of strings. I figure people on this
> list must have done this before.
>
> This code below is working, but could probably be written better. Does
> anyone have suggestions on cleaning up the 5 lines following the comment
> "OPTION A?. The two lines (commented out) following the comment "OPTION B"
> are what I would have expected to work, but throw 'Exception in thread
> "main" java.lang.ClassCastException: [Ljava.lang.Object;'
>
> Thanks in advance for any suggestions.
>
> -Ken
>
> - Begin client code listing ----------------------------
>
> package gov.noaa.eds.adicXmlRpcClient;
>
> import java.net.MalformedURLException;
> import java.net.URL;
> import java.util.ArrayList;
> import org.apache.xmlrpc.XmlRpcException;
> import org.apache.xmlrpc.client.XmlRpcClient;
> import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
>
> /**
> * This client will use the adicXmlRpcServer.
> */
> public class App {
>
> public static void main(String[] args) {
> System.out.println("Starting adicXmlRpcServer test");
>
> XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
> try {
> config.setServerURL(new URL("
> http://127.0.0.1:8084/adicXmlRpcServer/xmlrpc"));
> } catch (MalformedURLException ex) {
> ex.printStackTrace();
> }
> XmlRpcClient client = new XmlRpcClient();
> client.setConfig(config);
> Object[] params = new Object[] {new String("testDir")};
>
> try {
> /* OPTION A (next 5 lines):
> * This works, but looks ugly. Is there a better way to receive
> * an ArrayList of strings from the xml-rpc server?
> */
> Object[] result = (Object[]) client.execute("DirList.ls",
> params);
> ArrayList<String> dirListing = new ArrayList<String>();
> for (Object o : result) {
> dirListing.add(o.toString());
> }
>
> /* OPTION B (next 2 lines):
> * This doesn't work, but is the way I would like the code to
> * work. Java runtime doesn't like the cast.
> */
> // ArrayList<String> dirListing =
> // (ArrayList<String>) client.execute("DirList.ls",
> params);
>
> System.out.println("Listing Length=" + dirListing.size());
> System.out.println(" First 10:");
> for (int i = 0; i < 10; i++) {
> System.out.println(" " + dirListing.get(i));
> }
> } catch (XmlRpcException ex) {
> ex.printStackTrace();
> }
> }
> }
>
> - End client code listing ----------------------------
>
> In case it helps to know the server code, I'm sending an ArrayList<String>
> at the other end:
>
> - Begin server code listing ----------------------------
>
> /*
> * FILE: DirList.java
> */
> package gov.noaa.eds.adicXmlRpc;
>
> import java.util.ArrayList;
> import java.util.Random;
>
> /**
> * Provide directory listing functionality.
> */
> public class DirList {
>
> /**
> * Return a directory listing.
> * Currently generates made up names.
> * @param dirName directory name for which to get a listing.
> * @return a list of filenames for dirName
> */
> public ArrayList<String> ls(String dirName) {
> Random rng = new Random();
> int listLength = 2000;
> ArrayList<String> listing = new ArrayList<String>(listLength);
> for (int i = 0; i < listLength; i++) {
> int filenameLen = 1 + rng.nextInt(40);
> StringBuffer filename = new StringBuffer("sample_");
> for (int f = 0; f < filenameLen; f++) {
>
> filename.append("abcdefghijklmnopqrstuvwxyz".charAt(rng.nextInt(26)));
> }
> listing.add(filename.toString());
> }
> return listing;
> }
> }
>
> - End server code listing ----------------------------
>
> If anyone wants I can also post the XmlRpcServlet.properties, web.xml or
> maven pom.xml files, but those probably aren't needed.
>