【发布时间】:2010-12-13 13:54:19
【问题描述】:
我有一个项目,您可以在其中请求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;
}
}
更新:我的问题与 Referring to a non-final variable data inside an inner class 相同。我不知道异步调用工作机制。我仍然不知道如何通过我的response.getText() 来更新不属于我的 RestToInfoSession 类的标签有什么想法吗?
【问题讨论】:
-
如果我使用 google chrome 开发者工具,我可以看到我的 javascript 获得了 200 代码,并且收到的结果是我的 json 字符串。但结果未显示在标签中的浏览器应用程序中。这就是我放入入口点的内容:Label lab3 = new Label(); lab3.setText(new RestToInfoSession("2", "2010", "12").getJsonText());
标签: java javascript gwt