PDA

View Full Version : Read JSON from extern file



cabster
12 Nov 2009, 4:21 PM
I tried i tried but didn't work.


i'm trying to connect to a php file that generates json.
I need that data into a store. But with my current code the store seems to be empty :(

Any idea how to connect to an extern url (with get params)?

I don't know how to set parameters (GET parameters).




String url = "";
Method currentMethod = RequestBuilder.GET

ModelType type = new ModelType();
type.setRoot("data");
type.addField("racerName", "racerName");
type.addField("rankPoints", "rankPoints");
type.addField("clanName", "clanName");

ModelType params = new ModelType();
type.addField("page", "showRanking"); //This will be the get param

RequestBuilder builder = new RequestBuilder(currentMethod, url);

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

// need a loader, proxy, and reader
JsonLoadResultReader<ListLoadResult<ModelData>> reader = new JsonLoadResultReader<ListLoadResult<ModelData>>(
type);

this.setLoader(new BaseListLoader<ListLoadResult<ModelData>>(
proxy, reader));
this.getLoader().load();

ListStore<ModelData> store = new ListStore<ModelData>(this.getLoader());

Arno.Nyhm
13 Nov 2009, 6:52 AM
ModelType params = new ModelType();
type.addField("page", "showRanking"); //This will be the get param


typo: need to be: params.addField!




I don't know how to set parameters (GET parameters).


maybe just add it to the url?

cabster
13 Nov 2009, 7:22 AM
Yee thank you..already sorted the typo.

But i want to give them as parameter. That way i can change the method from GET to POST whenever i want.

All i want is to load Json data from an extern url into a grid.
If somebody have a code example please post it.

cabster
13 Nov 2009, 7:45 AM
This code works.
But with the parameters in the link.
I really need them to work with parameters



public class ExtGwtTester implements EntryPoint {

/**
* This is the entry point method.
*/
public void onModuleLoad() {


List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
columns.add(new ColumnConfig("racerName", "racerName", 100));
columns.add(new ColumnConfig("senderID", "senderID", 165));
columns.add(new ColumnConfig("clanName", "clanName", 50));

// create the column model
ColumnModel cm = new ColumnModel(columns);

// defines the xml structure
ModelType type = new ModelType();
type.setRoot("data");
type.addField("racerName", "racerName");
type.addField("senderID", "senderID");
type.addField("clanName", "clanName");


ModelType params = new ModelType();
type.addField("page", "showRanking");


// use a http proxy to get the data
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,"*/ranking.php?page=showRanking");
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);

ListStore<ModelData> store = new ListStore<ModelData>(loader);
final Grid<ModelData> grid = new Grid<ModelData>(store, cm);
grid.setBorders(true);
//grid.setLoadMask(true);
grid.getView().setEmptyText("Please hit the load button.");
//grid.setAutoExpandColumn("Sender");

ContentPanel panel = new ContentPanel();
panel.setFrame(true);
panel.setCollapsible(true);
panel.setAnimCollapse(false);
panel.setButtonAlign(HorizontalAlignment.CENTER);
//panel.setIcon(Examples.ICONS.table());
panel.setHeading("JSON Table Demo");
panel.setLayout(new FitLayout());
panel.add(grid);
panel.setSize(575, 350);

// add buttons
Button load = new Button("Load JSON", new SelectionListener<ButtonEvent>() {
public void componentSelected(ButtonEvent ce) {
loader.load();
System.out.println("button clicked");
}
});

loader.load();

panel.addButton(load);

RootPanel.get("test").add(panel);

}
}

sven
13 Nov 2009, 7:51 AM
You can use the loader.load(loadconfig).

In this loadconfig you can put in parameters.

Arno.Nyhm
13 Nov 2009, 8:05 AM
This code works.
But with the parameters in the link.
I really need them to work with parameters



did you try to search for RequestBuilder POST in this forum? ;-)

[2.0.1]jsonrpc and PagingToolBar
http://www.extjs.com/forum/showthread.php?p=394002#post394002

cabster
14 Nov 2009, 6:12 AM
did you try to search for RequestBuilder POST in this forum? ;-)

[2.0.1]jsonrpc and PagingToolBar
http://www.extjs.com/forum/showthread.php?p=394002#post394002

Thank you very much for the help guys.
The topic mentioned above helped.