Hello, I want to display information I retrieved from MySQL database into a table. I can see the JSON result in my console but I need it displayed in my app. Here is a part of my code:
byte[] data = cr.getResponseData();
Map response = (Stock.ParseJsonObject(data));
Stock.killNetworkAccess();
java.util.List statementResult = (java.util.List) response.get("StatementResult");
System.out.println("Statement Result:" + statementResult);
for (Object saveResult1 : statementResult) {
Map tempHash = (Map) saveResult1;
System.out.println(tempHash);
String result = tempHash.get("Status").toString();
String sellerName = tempHash.get("SellerName").toString();
String stockName = tempHash.get("StockName").toString();
if (result.equalsIgnoreCase("ok")) {
// System.out.print("Success");
System.out.println(stockName); //this works: I can see the list of stock names from my database
String[] columnNames = { "Seller's Name", "Stock Name", "Unit", "Price" };
Object[] dat = { sellerName, stockName };
Table tb = new Table(new DefaultTableModel(columnNames, (Object[][]) dat, false));
allrec.addComponent(tb);
}
}
The console shows this ->>>>>>
Statement Result:[{ID=1, SellerName=, StockName=Seven UP, Unit=23, Price=1, TimeCreated=0000-00-00 00:00:00, Status=Ok}, {ID=2, SellerName=, StockName=Seven UP, Unit=23, Price=1, TimeCreated=0000-00-00 00:00:00, Status=Ok}, {ID=3, SellerName=, StockName=Wallmart, Unit=76, Price=34, TimeCreated=0000-00-00 00:00:00, Status=Ok}, {ID=5, SellerName=, StockName=Africa Insurance, Unit=3, Price=23, TimeCreated=0000-00-00 00:00:00, Status=Ok}
{ID=1, SellerName=, StockName=Seven UP, Unit=23, Price=1, TimeCreated=0000-00-00 00:00:00, Status=Ok}
Seven UP
That shows the data is being retrieved from the database. However, when I try to get it in a table I get this error dialog that says:
Error: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to Ljava.lang.Object]
I am sure my error is somewhere here: ->>>>
String[] columnNames = { "Seller's Name", "Stock Name", "Unit", "Price" };
Object[] dat = { sellerName, stockName };
Table tb = new Table(new DefaultTableModel(columnNames, (Object[][]) dat, false));
I have these in another class called Stock:
public static Map ParseJsonObject(byte[] data) {
try {
JSONParser parser = new JSONParser();
Map response = parser.parseJSON(new InputStreamReader(new ByteArrayInputStream(data), "UTF-8"));
return response;
} catch (IOException ioe) {
}
return null;
}
public static void killNetworkAccess() {
NetworkManager.getInstance().shutdown();
}
Can someone please help me out? Thank you.