【发布时间】:2013-06-21 06:16:21
【问题描述】:
这是我目前的代码(这是使用 GWT):
private ArrayList<tObjects> getSuggestions(String query)
{
// Clear previous suggestions
Window.alert("Clearing arraylist");
arrayList.clear();
query = query.toUpperCase().replace(" ", "");
RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, "xmlfile.php?query="+query);
rb.setHeader("Content-Type", "application/x-www-form-urlencoded");
try
{
rb.sendRequest(null, new RequestCallback()
{
@Override
public void onResponseReceived(com.google.gwt.http.client.Request request, com.google.gwt.http.client.Response response)
{
Window.alert(response.getText());
// Do a lot of data processing here.
Window.alert("Adding to arraylist");
addToArrayList(data);
}
}
@Override
public void onError(com.google.gwt.http.client.Request request, Throwable exception)
{
// TODO Auto-generated method stub
}
});
}
catch(RequestException e)
{
Window.alert(e.toString());
}
Window.alert("Returning arraylist. "+arrayList.toString());
return arrayList;
}
我正确收到响应,但该方法在添加之前返回(空)arrayList。如果我删除 arrayList.clear(),在下一个 ajax 调用中我会看到上一个调用的结果。当我查看警报时,按以下顺序着火:
1) "清除数组列表。"
2) “返回数组列表。”
3) 来自 ajax 的正确响应的警报
4) "添加到数组列表"
在返回并完成该方法之前,该方法似乎没有等待 ajax 完成。在我到达 return 语句之前,如何让它等待 arrayList 的 ajax 和填充?
谢谢!
【问题讨论】: