我如何消耗返回列表的c#webMethod< lt; string>>通过我的Java客户?

发布于 2025-01-18 11:49:39 字数 1146 浏览 2 评论 0 原文

我在c#中有一个ASMX Web服务,其中webmethod getKeys()返回 list< list< string>>> ,但是当我通过我的java客户端消耗webmethod时, getKeys()是类型对象[] 。因此,我无法找到一种访问字符串的方法。理想情况下,我想将C#列表< list< string>> 放到arraylist< string []>但是我对更轻松的解决方案开放。我需要在JTable中显示内容。

对此的任何帮助或有关如何解决这种方法的提示,将不胜感激。

这是相关方法:

    public class ProgAss5WebService : System.Web.Services.WebService
        {
            [WebMethod]
            public List<List<string>> GetKeys()
            {
                return dal.GetKeysList();
            }
} //End of C# WebService class

public class Controller { //Java client
public void getKeys() {
        ArrayList<String[]> rowList = new ArrayList<String[]>();
        try {
            //This works
            Object[] myArr = proxy.getKeys();
            
            //This does not
            rowList = proxy.getKeys();
            
        }catch(RemoteException e) {
            e.printStackTrace();
        }
    }
}//End of Controller class

我仍在学习正确的方法,因此请告诉我是否需要更多信息并纠正我的问题中的任何错误。谢谢你!

I have an asmx webservice in C# where the webmethod GetKeys() returns a List<List<string>> however when I consume the webmethod in my java client through my proxy GetKeys() is of the type Object[]. Because of this I cannot figure out a way to access the strings. Ideally I would like to parse the C# List<List<string>> to an ArrayList<String[]> but I am open to easier solutions. I need to display the contents in a JTable.

Any help with this or tips on how I could solve this another way would be much appreciated.

Here are the relevant methods:

    public class ProgAss5WebService : System.Web.Services.WebService
        {
            [WebMethod]
            public List<List<string>> GetKeys()
            {
                return dal.GetKeysList();
            }
} //End of C# WebService class

public class Controller { //Java client
public void getKeys() {
        ArrayList<String[]> rowList = new ArrayList<String[]>();
        try {
            //This works
            Object[] myArr = proxy.getKeys();
            
            //This does not
            rowList = proxy.getKeys();
            
        }catch(RemoteException e) {
            e.printStackTrace();
        }
    }
}//End of Controller class

I am still learning the proper way of SO so please tell me if more information is needed and correct any mistakes in my question. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天暗了我发光 2025-01-25 11:49:39

您的 C# 方法正在后台进行反序列化和重新序列化。 Java 不知道 List> 是什么,因此它将其放入对象列表中。然后您可以投射该对象列表。可能有更快/更好的方法来做到这一点,但我不是 Java 专家,这应该足够好了:

for (i = 0; i < myArr.length; i++) {
   var x = ar[i];
   rowList.Add((List<String>)x;
}

Your C# method is being deserialized and reserialized in the background. Java does not know what a List<List<String>> is, so it makes it into a list of objects. You then cast that list of objects. There are probably faster/better ways to do this, but I'm not a Java expert and this should be good enough:

for (i = 0; i < myArr.length; i++) {
   var x = ar[i];
   rowList.Add((List<String>)x;
}
摘星┃星的人 2025-01-25 11:49:39

我找到了解决问题的方法。我更改了列表&gt;进入C#锯齿状阵列,似乎与Java中的多维阵列基本相同。我尝试了使用C#多维数组的解决方案,但是我收到了一个错误的消息,该误差表示不支持多维数组(不幸的是,我没有错误的邮件副本)。
通过正确阅读文档,我发现了如何使用锯齿状阵列,从而使代码非常简单。

public class ProgAss5WebService : System.Web.Services.WebService
        {
            [WebMethod]
            public string[][] GetKeys()
            {
                return dal.GetKeysList();
            }
} //End of C# WebService class

public class Controller { //Java client
public void getKeys() {
        ArrayList<String[]> rowList = new ArrayList<String[]>();
        try {
//This works
String[][] = proxy.GetKeys(); //Before, the proxy method would be of the wrong type

        //Then I parse the 2D string array to an ArrayList<String[]>    
        for (int rowIndex = 0; rowIndex < stringArr[0].length; rowIndex++) { // loops through the rows of the 2d stringArr

            String[] row = new String[stringArr.length];// For each row it creates a new string array containing the values

            for (int columnIndex = 0; columnIndex < stringArr.length; columnIndex++) { // loops through the cells of each row
                row[columnIndex] = stringArr[columnIndex][rowIndex].toString(); // casts the values of 2d stringArr
            }
            rowList.add(row); // adds the String[] to the arrayList
        }

        return rowList;
    }
}//End of Controller class

I found a way to solve my problem. I changed the List<List> into a C# jagged array which seems to be basically the same as a multidimensional array in Java. I tried the solution with a C# multidimensional array but I got an error messaage that said multidimensional arrays where not supported(unfortunately I don't have a copy of the error msg).
By properly reading the Documentaion I figured out how to use the jagged array which made the code very simple.

public class ProgAss5WebService : System.Web.Services.WebService
        {
            [WebMethod]
            public string[][] GetKeys()
            {
                return dal.GetKeysList();
            }
} //End of C# WebService class

public class Controller { //Java client
public void getKeys() {
        ArrayList<String[]> rowList = new ArrayList<String[]>();
        try {
//This works
String[][] = proxy.GetKeys(); //Before, the proxy method would be of the wrong type

        //Then I parse the 2D string array to an ArrayList<String[]>    
        for (int rowIndex = 0; rowIndex < stringArr[0].length; rowIndex++) { // loops through the rows of the 2d stringArr

            String[] row = new String[stringArr.length];// For each row it creates a new string array containing the values

            for (int columnIndex = 0; columnIndex < stringArr.length; columnIndex++) { // loops through the cells of each row
                row[columnIndex] = stringArr[columnIndex][rowIndex].toString(); // casts the values of 2d stringArr
            }
            rowList.add(row); // adds the String[] to the arrayList
        }

        return rowList;
    }
}//End of Controller class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文