I am parsing JSON data and currently it can display in a dialog box. But I'm having trouble displaying it in the Pane. I'm pretty new to GWT and would appreciate any guidance. I provided the appropriate methods below.
private void jsonPane(String jsonText, final FlowPanel jsonSource) {
jsonText = jsonText.replaceAll(">", ">");
Label json = new HTML("<pre>" + jsonText + "</pre>", false);
parseData();
jsonSource.add(json);
}
// Parsing the JSON Data
private void parseData() {
RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET, "/jsongwtexample/greet");
try {
requestBuilder.sendRequest(null, new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
JSONValue jsonValue = JSONParser.parseStrict(response.getText());
JSONObject weatherObject = jsonValue.isObject();
JSONArray jsonArray = weatherObject.get("Weather").isArray();
StringBuilder builder = new StringBuilder("** TODAYS WEATHER** \n");
builder.append(jsonArray.get(0).isObject().get("Town").isString().stringValue()).append(" ");
builder.append(jsonArray.get(0).isObject().get("Weather").isString().stringValue());
builder.append("\n");
builder.toString();
}
@Override
public void onError(Request request, Throwable exception) {
Window.alert("Some error occurred: " + exception.getMessage());
}
});
} catch (RequestException e) {
e.printStackTrace();
}
}
I would use a plain label, rather than an HTML one, to prevent malicious HTML-injection.
private Label label = new Label();
You can style the label to display just like a <pre>
:
public void onModuleLoad() {
RootPanel.get().add(label);
label.getElement().getStyle().setWhiteSpace(WhiteSpace.PRE);
}
Inside your parseData()
method you need to set the label's text:
@Override
public void onResponseReceived(Request request, Response response) {
...
label.setText(builder.toString());
}