我在GWT中编写一个servlet,我并不真正理解在函数中声明为global的数据变量发生了什么。也许这是一个更常见的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声明使用后,数据的内容在哪里?
非常感谢你的帮助。
答案 0 :(得分:2)
在异步调用响应之后调用onResponseReceived
。以下语句很可能在收到响应之前执行。
System.out.println("3Table has colums " + data.getNumberOfColumns());
System.out.println("3Table has rows " + data.getNumberOfRows());
使用一些状态标志来检查最后打印尺寸之前的回叫状态。