在GWT中使用JSONParser的问题 [英] Problems using JSONParser with GWT
问题描述
我有一个简单的GWT应用程序,需要从另一台服务器获取一些JSON数据。我遵循了几个教程来达到这一点。当我尝试编译它时,出现错误
I have a simple GWT app that needs to get some JSON data from another server. I've followed a couple of tutorials to get to this point. When I try to compile it, I get errors
[错误]第44行:没有源代码可用于类型
com .google.gwt.json.client.JSONValue;你忘了继承
所需的模块吗?
[错误]第44行:没有源代码可用于com.google.gwt.json.client.JSONParser类型;你忘了继承
所需的模块吗?
[错误]第46行:没有源代码可用于com.google.gwt.json.client.JSONArray类型;你忘了继承
所需的模块吗?
[错误]第49行:没有源代码可用于com.google.gwt.json.client.JSONObject类型;你忘了继承
所需的模块吗?
[ERROR] Line 44: No source code is available for type com.google.gwt.json.client.JSONValue; did you forget to inherit a required module? [ERROR] Line 44: No source code is available for type com.google.gwt.json.client.JSONParser; did you forget to inherit a required module? [ERROR] Line 46: No source code is available for type com.google.gwt.json.client.JSONArray; did you forget to inherit a required module? [ERROR] Line 49: No source code is available for type com.google.gwt.json.client.JSONObject; did you forget to inherit a required module?
我知道我必须添加
I know I had to add
<inherits name="com.google.gwt.http.HTTP" />
添加到我的.gwt.xml文件中,但找不到要添加的内容认识JSON的东西。请问我错过了什么?
to my .gwt.xml file, but couldn't figure out what to add to get it to recognize the JSON stuff. What am I missing, please?
相关代码:
Relevant code:
private SearchResult[] parseResponse(String jsonResponse) {
ArrayList<SearchResult> retArray = new ArrayList<SearchResult>();
JSONValue jval = JSONParser.parseStrict(jsonResponse);
JSONArray resultArray = jval.isArray();
for(int i=0; i<resultArray.size(); i++) {
JSONObject resultObj = resultArray.get(i).isObject();
String title = resultObj.get("title").isString().stringValue();
JSONArray roleArray = resultObj.get("roles").isArray();
String roleNames = new String();
for(int j=0; j< roleArray.size(); j++) {
if(roleArray.get(j).isNumber().doubleValue() == 1.0) {
// this role is present
String currRole = Constants.getRoleNameForNum(j);
roleNames += currRole;
}
}
SearchResult sr = new SearchResult(title, roleNames);
retArray.add(sr);
}
return retArray.toArray(new SearchResult[0]);
}
private void doSearch() {
clearTable();
final String searchTerms = searchTextBox.getText().toLowerCase().trim();
searchTextBox.setFocus(true);
final int roleNum = roleChooserBox.getSelectedIndex();
final String roleName = roleChooserBox.getItemText(roleNum);
String url = JSON_URL + "?" + ROLE_TXT + roleNum + "&" + QUERY_TXT + "'" + searchTerms + "'";
url = URL.encode(url);
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
try {
Request request = builder.sendRequest(null, new RequestCallback() {
@Override
public void onError(Request request, Throwable exception) {
displayError("Couldnt' retrieve JSON");
}
@Override
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
SearchResult[] results = parseResponse(response.getText());
updateTable(results, roleName);
} else {
displayError("Couldn't retrieve JSON (" + response.getStatusText()
+ ")");
}
}
});
} catch (RequestException e) {
displayError("Couldn't retrieve JSON");
}
`
推荐答案
After进一步的试验和错误,加入
After further trial and error, adding
<inherits name="com.google.gwt.json.JSON" />
添加到我的.gwt.xml文件中。我很失望,我无法找到解释该文档的任何信息。这将节省很多时间。
to my .gwt.xml file did the trick. I'm disappointed that I couldn't find any information in the documentation explaining that. It would have saved a lot of time.
这篇关于在GWT中使用JSONParser的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!