PDA

View Full Version : Yet Another Grid Question... JSON POST request not working



sgt
30 Jun 2009, 10:53 AM
I have a Grid problem which is probably due to my misunderstanding of how Grids must be populated. I have more or less based this on examples I have found, but it is not working:



public CustSearchGrid (
String name, String address, String phonenumber,
String ageGroup, Date fromDate, Date toDate )
{
setLayout(new FlowLayout(0));

// Create the list of columns for the model
List<ColumnConfig> cols = new ArrayList<ColumnConfig>();
cols.add(new ColumnConfig("name", "Name", 40));
cols.add(new ColumnConfig("address", "Address", 40));
cols.add(new ColumnConfig("phonenumber", "Cell Number", 20));
ColumnModel cModel = new ColumnModel(cols);

ModelType modelType = new ModelType();
modelType.setRoot("rows");
modelType.setRecordName("name");
modelType.setTotalName("total");
modelType.addField("name");
modelType.addField("address");
modelType.addField("phonenumber");

// RequestBuilder
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode("/searchCustomers"));

String reqData = createRequestList(name, address, phonenumber, ageGroup, fromDate, toDate).toString(); // This gives JSON string such as: "['namehere', 'addr', '12313123', '26-49', 'Wed Jun 10 00:00:00 SAST 2009', 'Tue Jun 30 00:00:00 SAST 2009']"

builder.setRequestData(reqData);

try {
builder.setCallback (
new RequestCallback(){

@Override
public void onError(Request request, Throwable exception) {
// TODO Auto-generated method stub
Window.alert("Got error from req: " + request.toString());
}

@Override
public void onResponseReceived(Request request, Response response) {
// TODO Auto-generated method stub
}});

} catch (NullPointerException e) {
}

HttpProxy<String> proxy = new HttpProxy<String>(builder);

JsonReader<PagingLoadConfig> jsonReader = new JsonReader<PagingLoadConfig>(modelType);

final BaseListLoader<ListLoadResult<ModelData>> loader = new BaseListLoader<ListLoadResult<ModelData>>(proxy, jsonReader);
ListStore<ModelData> store = new ListStore<ModelData>(loader);
final Grid<ModelData> grid = new Grid<ModelData>(store, cModel);
grid.setBorders(false);
grid.setHeight("255px");

ContentPanel panel = new ContentPanel();
panel.setFrame(false);
panel.setButtonAlign(HorizontalAlignment.CENTER);
panel.setHeaderVisible(false);
panel.setBodyBorder(false);
panel.add(grid);

GridView v = new GridView();
v.setEmptyText("Found no results");
v.setForceFit(true);
grid.setView(v);


loader.load();

add(panel);

}


Upon a button being pressed, I instantiate the CustSearchGrid class and it connects to the HTTP web service
It then POSTs "sortField=null&sortDir=NONE" and nothing else. I want it to post my JSON string.
If I force the web service to return e.g. {'total': '1', "rows":[{"name":'John', "address":'No address', "phonenumber":'1234'}]} then it does update the grid.

What am I not understanding correctly? I see there is a book about EXT-GWT on Apress, and I'm going to order it ASAP, but it would be extremely helpful to get through this little problem first.

Thanks!

tomasi
2 Jul 2009, 5:21 PM
Try builder.setHeader("Content-type", "application/x-www-form-urlencoded");
set this after builder when you want to use POST method to trans data.

I'm not sure this would work or not. Hopefully this could solve your problem.

sgt
11 Jul 2009, 11:35 AM
I asked on #extgwt on FreeNode IRC network and got the following answer:
< sven2>the httpproxy overrides the requestdata
< sven2> you need to add a beforeloadevent to the loader and change the loadconfig (add a field with your requestdata)

So basically what he means is that the builder.setRequestData() is ignored in this case. To solve it, I can create a BaseListLoadConfig (which implements LoadConfig and ModelData) like this:



...
BaseListLoadConfig config = new BaseListLoadConfig ();

config.set("name", "whatevernamehere");
loader.load(config);
...


The POST will now be "sortField=&name=whatevernamehere&sortDir=NONE" just as I wanted.


Thanks!