简体   繁体   English

将Java Arraylist返回值从Java WebService转换为C#Arraylist

[英]Convert Java Arraylist return from Java WebService to C# Arraylist

In my C# program, I would like to consume the Java Web Service Method which replies the java.Util.ArrayList. 在我的C#程序中,我想使用Java Web Service方法来回复java.Util.ArrayList。 I don't know how to convert the Java ArrayList to C# ArrayList. 我不知道如何将Java ArrayList转换为C#ArrayList。

This is Java Codes 这是Java代码

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

This is C# Codes 这是C#代码

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

I got the following Error in C# 我在C#中收到以下错误

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

How can I cast these Java ArrayList to C# ArrayList 如何将这些Java ArrayList转换为C#ArrayList

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). 绝对不是考虑到Web服务互操作性编写的Java类(Java方法的确不是那么好-它应该返回List,而不是ArrayList)。 If you can change it, try changing it to an array (return type: Message[]). 如果可以更改,请尝试将其更改为数组(返回类型: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). 我认为,即使假设数组运行顺利(可能不会),这也只会带您回到询问该Message类的处理方式(我是否提到这不是在考虑Web Service互操作性的情况下编写的?)。

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. 如果您无法更改Java类,则可能很不走运,但是您必须调用Web服务并获取原始XML,然后了解如何解析它才能知道您的选择。

Java中的ArrayList和C#中的ArrayList是非常不同的东西,您需要以一种双方都可以轻松理解的格式(例如XML)共享数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM