我正在編寫一個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 colums
1表具有0行
2表中有2 colums
2表有10行
3表具有0 colums
3表具有0行
哪裏數據內容的onResponseReceived聲明中使用後它去了?
非常感謝您的幫助。
你期望輸出什麼,它與你得到的有什麼不同? –