PDA

View Full Version : Populate a combo from JSON data



babbarkrishan
23 Dec 2010, 3:17 AM
Hi,

I want to populate a combo box using REST which returns data in JSON format. I tried with below given code but it is giving ClassCastException. Please help me to solve this, i would appreciate your help very much.



// Format of JSON return from server on calling of REST
{"response":{"entityName":null,"statusCode":0,"statusMessage":"SUCCESS","entityId":null,"parentName":null},"users":["Krishan","Chris"]}



// Calling REST method and parsing JSON data. I would like to fetch "users" Krishan and Chris from above JSON and populate the same in a combo box
ModelType type = new ModelType();
type.setRoot("response");
type.addField("users", "users");

RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(fetchAllUsersUrl));
HttpProxy<String> proxy = new HttpProxy<String>(builder);

JsonLoadResultReader<ListLoadResult<ModelData>> reader = new JsonLoadResultReader<ListLoadResult<ModelData>>(type);

final BaseListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(proxy, reader);
final ListStore<ModelData> store = new ListStore<ModelData>(loader);
loader.load();

loader.addLoadListener(new LoadListener() {
public void handleEvent(LoadEvent e) {
EventType type = e.getType();
if (type == Loader.Load) {
populateUsers(store);
}
}
});

SimpleComboBox<String> userCombo = new SimpleComboBox<String>();
public void populateUsers(ListStore<ModelData> store) {

Iterator<ModelData> usersIterator = store.getModels().iterator();

while (usersIterator.hasNext()) {
ModelData user = usersIterator.next();
userCombo.add((String)user.get("users"));
}

}Thanks
Krishan Babbar

sven
23 Dec 2010, 3:19 AM
I tried with below given code but it is giving ClassCastException

What is the stack trace? Also you should not use SimpleComboBox here but ComboBox directly, as it is no longer simple.

babbarkrishan
23 Dec 2010, 3:46 AM
What is the stack trace? Also you should not use SimpleComboBox here but ComboBox directly, as it is no longer simple.

Stack is given below:
java.lang.ClassCastException: com.google.gwt.json.client.JSONObject cannot be cast to com.google.gwt.json.client.JSONArray
at com.extjs.gxt.ui.client.data.JsonReader.read(JsonReader.java:54)
at com.extjs.gxt.ui.client.data.HttpProxy$1.onResponseReceived(HttpProxy.java:75)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:287)
at com.google.gwt.http.client.RequestBuilder$1.onReadyStateChange(RequestBuilder.java:395)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:126)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:214)
at sun.reflect.GeneratedMethodAccessor69.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:126)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:289)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)
at com.google.gwt.user.client.Window.alert(Window.java)

at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessagesWhileWaitingForReturn(BrowserChannelServer.java:326)
at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:207)
at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:126)
at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561)
at com.google.gwt.dev.shell.ModuleSpace.invokeNativeObject(ModuleSpace.java:269)
at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeObject(JavaScriptHost.java:91)
at com.google.gwt.core.client.impl.Impl.apply(Impl.java)
at com.google.gwt.core.client.impl.Impl.entry0(Impl.java:214)
at sun.reflect.GeneratedMethodAccessor69.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.MethodDispatch.invoke(MethodDispatch.java:71)
at com.google.gwt.dev.shell.OophmSessionHandler.invoke(OophmSessionHandler.java:157)
at com.google.gwt.dev.shell.BrowserChannelServer.reactToMessages(BrowserChannelServer.java:281)
at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:531)
at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:352)
at java.lang.Thread.run(Unknown Source)


Ok, I will try with ComboBox also

sven
23 Dec 2010, 4:13 AM
Your root is not "response". The format you are using here is not supported be the default json reader.