如果你只需要一個硬編碼的值列表來直觀地測試你的小部件,你可以簡單地把這些值放在一個字符串數組中,並從那裏加載它。或者你可以使用RequestBuilder從服務器獲取字符串。你可以在你的war目錄中保存一個簡單的文件(CSV,XML,JSON等),並使用Request Builder構建這個文件。從GWT developer guide
示例代碼:
import com.google.gwt.http.client.*;
...
String url = "http://www.myserver.com/getData?type=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
try {
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
// Couldn't connect to server (could be timeout, SOP violation, etc.)
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
// Process the response in response.getText()
} else {
// Handle the error. Can get the status text from response.getStatusText()
}
}
});
} catch (RequestException e) {
// Couldn't connect to server
}
確保您繼承了HTTP模塊:
<inherits name="com.google.gwt.http.HTTP" />
是啊,要去硬編碼文件的情侶。 – DemiSheep