我在 GWT 中编写一个 servlet,但我真的不明白函数中声明为全局的数据变量发生了什么。也许这是一个与在动态函数声明中使用变量相关的更普遍的 Java 问题,但我找不到答案。
这是我的代码:
private AbstractDataTable fetchDataFromServer() {
final DataTable data = DataTable.create();
System.out.println("1 Table has " + data.getNumberOfColumns() + " columns");
System.out.println("1 Table has " + data.getNumberOfRows() + " rows");
try {
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, "/mongo.json");
rb.setCallback(new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
JSONValue value = JSONParser.parse(response.getText());
JSONObject productsObj = value.isObject();
JSONArray radiation = productsObj.get("ShortWave_Rad").isArray();
if (radiation != null) {
data.addColumn(ColumnType.NUMBER, "Horizon");
data.addColumn(ColumnType.NUMBER, "Value");
System.out.println("Values " + radiation.size());
for (int i=0; i<=radiation.size()-1; i++) {
JSONObject productObj = radiation.get(i).isObject();
data.addRows(radiation.size());
data.setValue(i, 0, productObj.get("horizon").isNumber().doubleValue());
data.setValue(i, 1, productObj.get("value").isNumber().doubleValue());
}
System.out.println("2 Table has " + data.getNumberOfColumns() + " columns");
System.out.println("2 Table has " + data.getNumberOfRows() + " rows");
}
}
@Override
public void onError(Request request, Throwable exception) {
Window.alert("Error occurred" + exception.getMessage());
}
});
rb.send();
}
catch (RequestException e) {
Window.alert("Error occurred" + e.getMessage());
}
System.out.println("3 Table has " + data.getNumberOfColumns() + " columns");
System.out.println("3 Table has " + data.getNumberOfRows() + " rows");
return data;
}
这是我得到的输出:
1 表有 0 列
1 表有 0 行
2 表有 2 列
2 表有 10 行
3 表有 0 列
3 表有 0 行
onResponseReceived 声明使用后数据的内容去了哪里?
非常感谢您的帮助。