[英]GWT and REST (jax-rs)
我有一個項目,您可以在其中請求由jax-rs
以json
格式提供的資源。 當我查詢json
出現的其余 URL 時,瀏覽器中的一切正常。
現在我希望我的 GWT 項目請求這些資源並處理它們並在我的界面中顯示它們。 我發現這樣做的最簡單方法是使用請求構建器和覆蓋層。 代碼較低。 問題是,當代碼運行時,它似乎永遠不會進入實際的 RequestCallback()。 狀態字符串永遠不會改變。 我認為這可能是一個 SOP,所以我添加了<add-linker name="xs"/>
但仍然不起作用。 有什么理想嗎?
package com.workoutcell.client;
//import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.http.client.*;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
/**
*
* @author
*/
public class RestToInfoSession{
String queryReturn = null;
JsArray<InfoJSO> arrayOfInfo = null;
String host = "http://localhost:8080/mysite";
String restModule = "/calendar/getinfo";
String id = null;
String year = null;
String month = null;
String status = "Not Initialized";
public RestToInfoSession(String id, String year, String month){
this.id =id;
this.year = year;
this.month = month;
String url = host + restModule + "/"+this.id + "/"+this.year + "/"+this.month;
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
status = "Initialized at Url " + builder.getUrl();
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.)
status = "Error on connecting to Server";
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
// arrayOfInfo = jsonToJsArray(response.getText());
status = "JSON has been Fetched. Result is:" + response.getText();
} else if(0 == response.getStatusCode()) {
status = "Error is 0";
} else {
status = "Error in JSON Request:" + response.getStatusCode();
//response.getStatusText();
}
}
});
} catch (RequestException ex) {
status = "Error in Request Builder Startup";
}
}
//get an jso object in array
private final native JsArray<InfoJSO> jsonToJsArray(String json) /*-{
return eval(json);
}-*/;
public JsArray<InfoJSO> getInfoArray (){
return arrayOfInfo;
}
}
更新:我的問題與在內部類中引用非最終變量數據相同。 我不知道異步調用的工作機制。 我仍然不知道如何通過我的response.getText()
來更新不屬於我的 RestToInfoSession 類的標簽有什么想法嗎?
考慮使用RestyGWT項目。 它將使調用 JAXRS JSON 資源像使用 GWT-RPC 一樣簡單。 此外,您通常可以在客戶端重復使用來自服務器端的相同請求響應 DTO。
我放置了一個計時器,每 1000 毫秒檢查一次我的 json 字符串是否已從 null 更新為 xhttp 請求的數據。 這有效,但我覺得有一種更優雅的方法可以解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.