'How do I consume c# WebMethod that returns List<List<string>> through my java client?
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!
Solution 1:[1]
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;
}
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chrotenise |
| Solution 2 | dkajo |
