1

In my C# program, I would like to consume the Java Web Service Method which replies the java.Util.ArrayList. I don't know how to convert the Java ArrayList to C# ArrayList.

This is Java Codes

@WebMethod
@WebResult(name = "GetLastMessages")
@SuppressWarnings("unchecked")
public ArrayList<Message> getLastMessages(@WebParam(name = "MessageCount")int count) {
    ....
            ....
    return messages;
}

This is C# Codes

MessagesService.MessagesService service = new MessagesService.MessagesService();
System.Collections.ArrayList arr = (System.Collections.ArrayList)service.getLastMessages(10);

I got the following Error in C#

Cannot convert type 'WindowsFormsApplication1.MessagesService.arrayList' to 'System.Collections.ArrayList'  

How can I cast these Java ArrayList to C# ArrayList

2 Answers 2

1

That is definitely not a Java class written with Web Service interoperability in mind (nor is it really that good of a Java method - it should be returning List, not ArrayList). If you can change it, try changing it to an array (return type: Message[]). Very easy to do, just change the return line to read:

  return messages.toArray(new Message[0]);

I think that will just lead you right back to SO asking what to do with that Message class (did I mention this was not written with Web Service interoperability in mind?), even assuming the array goes smoothly (it may not).

If you can't change the Java class, you are probably very out of luck, but you would have to call the web service and get the raw XML and see what you can do about parsing it to know what your options are.

Sign up to request clarification or add additional context in comments.

Comments

0

An ArrayList in Java and an ArrayList in C# are very different things, you need to share the data in a format that can easily be understood by both parties, e.g. XML.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.