MH.
MH.

Reputation: 557

GWT: Handle JSON data after submitting http request

I’m working on a GWT web application that needs to communicate with a common web server. Unfortunately, the server only supports PHP, so I can’t use GWT RPC. That’s why I want to use a simple PHP script on server side, which returns all the necessary data in JSON format. Because I’m fairly new to GWT, my code is based on this example:
http://code.google.com/p/google-web-toolkit-doc-1-5/wiki/GettingStartedJSON
The PHP script seems to work fine. Firebug shows me the returned JSON data and the port 200: Response:

[{"key1":"k1","value1":"v1"},{"key2":"k2","value2":"v2"},{"key2":"k3","value3":"v3]

However, the response is never processed further. Here is my code:

private static final String JSON_URL = "http://www.myUrl/myScript.php";

public HashMap<String, Integer> loadCalendarTable(String p1, String p2) {
    table = new HashMap<String, Integer>();
    String url = JSON_URL+"?p1="+p1+"&p2="+p2;
    url = URL.encode(url);

    RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
    try {
        Request request = builder.sendRequest(null, new RequestCallback() {
            public void onError(Request request, Throwable exception) {
                Window.alert("Couldn't retrieve JSON");
            }
            public void onResponseReceived(Request request, Response response) {
                if (200 == response.getStatusCode()) {
                    try {
                        // parse the response text into JSON
                        JSONValue jsonValue = JSONParser.parse(response.getText());
                        JSONArray jsonArray = jsonValue.isArray();
                        if (jsonArray != null) {
                            HashMap<String, Integer> hm = updateTable(jsonArray); 
                        } 
                        else
                            throw new JSONException();
                    } 
                    catch (JSONException e) {
                        Window.alert("Could not parse JSON");
                    }
                } 
                else 
                    Window.alert("Couldn't retrieve JSON (" + response.getStatusText() + ")");
            }
        });
        //(*)
    } 
    catch (RequestException e) {
        Window.alert("Couldn't retrieve JSON");
    }
    return table;
}

private HashMap<String, Integer> updateTable(JSONArray array) {
//do something
return table;}

By executing the application on the web server, there occurs no exception and no alert pops up. By using some alerts (which I omitted in the code above for readability), I noticed that the try-statement in new RequestBuilder() is executed. Another alert at (*) shows, that the try-statement is passed. (No exception occurs, as mentioned before). Obviously, the method onResponseReceived() is never executed. I never called this method, so this could be the reason for my problem. But then, I don’t understand where I should call onResponseReceived().

Remark: I omitted my PHP script, because that’s actually the same as showed in the online example (http://code.google.com/p/google-web-toolkit-doc-1-5/wiki/GettingStartedJSON). Besides, the script seems to work properly.

Upvotes: 2

Views: 2098

Answers (1)

Colin Alworth
Colin Alworth

Reputation: 18356

How do you know that onResponseRecieved is not being executed? This should be called when your script.php returns JSON data. Assuming your server sends back a 200 status code, the response might be parsed, but no nothing is done with the data

try {
  // parse the response text into JSON
  JSONValue jsonValue = JSONParser.parse(response.getText());
  JSONArray jsonArray = jsonValue.isArray();
  if (jsonArray != null) {
    HashMap<String, Integer> hm = updateTable(jsonArray); 
    // Now what? hm isn't actually used for anything...
  } else {
    throw new JSONException();
  }

From the value returned at the end of the method, table is apparently important, but it will be returned, empty before the onResponseRecieved callback is ever invoked. This is because all GWT Ajax calls are asynchronous - the script won't stop running while waiting for the server to resume. Create an alert with response.getTest() in the if (jsonArray != null) block, and you may find that this code is getting called after all. If so, you've fallen victim to That's not async - it is considered generally good JavaScript practice (and all but mandatory in GWT) to wait for results to arrive when ready, and to keep executing as normal in the meantime.

Upvotes: 1

Related Questions