Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems using JSONParser with GWT

Tags:

java

json

gwt

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

[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" />

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");
        }
            `
like image 414
betseyb Avatar asked Feb 10 '15 21:02

betseyb


1 Answers

After further trial and error, adding

<inherits name="com.google.gwt.json.JSON" />

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.

like image 184
betseyb Avatar answered Oct 19 '22 14:10

betseyb