PDA

View Full Version : Problem in Using JSON



Balaji Chopparapu
8 Sep 2009, 1:01 AM
Hi,


I am trying to use JSON to display the set of records in a grid . But I am facing problem with “com.extjs.gxt.ui.client.data.JsonReader”. It is giving me the below exception.

java.lang.NullPointerException
at com.extjs.gxt.ui.client.data.JsonReader.read(JsonReader.java:55)
at com.extjs.gxt.ui.client.data.HttpProxy$1.onResponseReceived(HttpProxy.java:76)
at com.google.gwt.http.client.Request.fireOnResponseReceivedImpl(Request.java:264)
at com.google.gwt.http.client.Request.fireOnResponseReceivedAndCatch(Request.java:236)
at com.google.gwt.http.client.Request.fireOnResponseReceived(Request.java:227)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.dev.shell.MethodAdaptor.invoke(MethodAdaptor.java:103)
at com.google.gwt.dev.shell.ie.IDispatchImpl.callMethod(IDispatchImpl.java:126)
at com.google.gwt.dev.shell.ie.IDispatchProxy.invoke(IDispatchProxy.java:155)
at com.google.gwt.dev.shell.ie.IDispatchImpl.Invoke(IDispatchImpl.java:294)
at com.google.gwt.dev.shell.ie.IDispatchImpl.method6(IDispatchImpl.java:194)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(COMObject.java:117)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:1925)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2966)
at com.google.gwt.dev.SwtHostedModeBase.processEvents(SwtHostedModeBase.java:235)
at com.google.gwt.dev.HostedModeBase.pumpEventLoop(HostedModeBase.java:558)
at com.google.gwt.dev.HostedModeBase.run(HostedModeBase.java:405)
at com.google.gwt.dev.GWTShell.main(GWTShell.java:140)

JSON Data:

{
"osim.getOSIMImagesResponse":
{
"item":[
{"batchFile":"Winxp.bat","codePage":"1252 - Western European","comment":"This is comment for Winxp","name":"WinXPImage 0","OS":"Winxp 0","SDPackName":"SDPackName","type":"WINXXP"},
{"batchFile":"Winxp.bat","codePage":"1252 - Western European","comment":"This is comment for Winxp","name":"WinXPImage 1","OS":"Winxp 1","SDPackName":"SDPackName","type":"WINXXP"}
]
}
}

My UI Class:

// defines the json structure
ModelType type = new ModelType();
type.setRoot("item");
type.addField("name", "name");
type.addField("batchFile", "batchFile");
type.addField("OS", "OS");
type.addField("codePage", "codePage");
type.addField("comment", "comment");
type.addField("SDPackName", "SDPackName");
type.addField("type", "type");


// use a http proxy to get the data
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,"http://localhost:9001/OSIMJSONService/osimimages" );
HttpProxy<String> proxy = new HttpProxy<String>(builder);
// need a loader, proxy, and reader
JsonLoadResultReader<ListLoadResult<ModelData>> reader = new JsonLoadResultReader<ListLoadResult<ModelData>>(type);
final BaseListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(
proxy, reader);


I believe there is something wrong in Model Type. Please Suggest

Thanks & Regards
Balaji Chopparapu

Balaji Chopparapu
8 Sep 2009, 9:05 PM
Any suggestions?

~Balaji

Colin Alworth
8 Sep 2009, 10:31 PM
JSON Data:

{
"osim.getOSIMImagesResponse":
{
"item":[
{"batchFile":"Winxp.bat","codePage":"1252 - Western European","comment":"This is comment for Winxp","name":"WinXPImage 0","OS":"Winxp 0","SDPackName":"SDPackName","type":"WINXXP"},
{"batchFile":"Winxp.bat","codePage":"1252 - Western European","comment":"This is comment for Winxp","name":"WinXPImage 1","OS":"Winxp 1","SDPackName":"SDPackName","type":"WINXXP"}
]
}
}

My UI Class:

// defines the json structure
ModelType type = new ModelType();
type.setRoot("item");
type.addField("name", "name");
type.addField("batchFile", "batchFile");
type.addField("OS", "OS");
type.addField("codePage", "codePage");
type.addField("comment", "comment");
type.addField("SDPackName", "SDPackName");
type.addField("type", "type");

Just guessing here, but the root of your json is "osim.getOSIMImagesResponse", not "item". If you cant change how the server sends the json, you might not be able to use this class as you want to.

But this makes sense - you are trying to send n elements of data wrapped in an object, wrapped in another object. Wrapping it twice doesnt make too much sense.

Quickly reading the code, another option might be to start unwrapping the object before passing it into the reader - I can give a sample if you can show your builder.sendRequest and callback.

BIGANDYT
8 Sep 2009, 11:47 PM
based on the above post it does look like you have an issue with your root, however, i dont think thats a major problem, as ya workaround would be to just clean your json string client side to remove {
"osim.getOSIMImagesResponse": and the trailing }.

if it always adds that.



String json = "{\"osim.getOSIMImagesResponse\":{\"item\":[{\"batchFile\":\"Winxp.bat\",\"codePage\":\"1252 - Western European\",\"comment\":\"This is comment for Winxp\",\"name\":\"WinXPImage 0\",\"OS\":\"Winxp0\",\"SDPackName\":\"SDPackName\",\"type\":\"WINXXP\"},{\"batchFile\":\"Winxp.bat\",\"codePage\":\"1252 - Western European\",\"comment\":\"This is comment for Winxp\",\"name\":\"WinXPImage 1\",\"OS\":\"Winxp 1\",\"SDPackName\":\"SDPackName\",\"type\":\"WINXXP\"}]}}";
json = json.replace("{\"osim.getOSIMImagesResponse\":", "");
json = json.substring(0,json.length()-1);

Balaji Chopparapu
9 Sep 2009, 10:11 PM
Hi Alworth & BIGANDYT,

Thanks for your suggestions. I modified my server code to trim the addition JSON Object and the UI classes are handling well.

My modified JSON:

{"osimImage":[{"batchFile":"Winxp.bat","codePage":"1252 - Western European","comment":"This is comment for Winxp","name":"WinXPImage 0","OS":"Winxp 0","SDPackName":"SDPackName","type":"WINXXP"},{"batchFile":"Winxp.bat","codePage":"1252 - Western European","comment":"This is comment for Winxp","name":"WinXPImage 1","OS":"Winxp 1","SDPackName":"SDPackName","type":"WINXXP"}]}

I have choosen restful webservice with Jersey frame work

I am very new to Ext and nice to see a active community. Hopefully we will have a nice time. But I am curios to solve my problem without modifying my server code.


Thanks & Regards
Balaji Chopparapu

Colin Alworth
9 Sep 2009, 10:13 PM
Post the request callback (or something which approximates it) and I would be happy to try and help find a client solution.

Balaji Chopparapu
9 Sep 2009, 10:18 PM
Thanks Alworth,

This my eariler serve side JSON Response:

{"osim.getOSIMImagesResponse":{"item":[{"batchFile":"Winxp.bat","codePage":"1252 - Western European","comment":"This is comment for Winxp","name":"WinXPImage 0","OS":"Winxp 0","SDPackName":"SDPackName","type":"WINXXP"},{"batchFile":"Winxp.bat","codePage":"1252 - Western European","comment":"This is comment for Winxp","name":"WinXPImage 1","OS":"Winxp 1","SDPackName":"SDPackName","type":"WINXXP"}]}}

Colin Alworth
9 Sep 2009, 11:16 PM
Sorry, I may not have been clear - the callback function, not the data itself. Something like


request.sendRequest(data, new RequestCallback() {
public void onResponseReceived(Request req, Response resp) {
// callback code goes here
}
public void onError(Request req, Throwable ex) {
// error handling
}
});