// Here we call JSON.php
require_once("
JSON.php");
// Then we do the query to the MySQL DB, and fetch the results
$conector = mysql_connect('127.0.0.1', 'root', 'juan') or die(mysql_error());
mysql_select_db('JSONPHP') or die(mysql_error());
$sqlQuery = "SELECT * FROM directory WHERE name LIKE '". $_REQUEST['tosearch']. "%'";
$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
$i = 0;
while($row = mysql_fetch_array($dataReturned)){
// We fill the $value array with the data.
$value{"item"}{$i}{"Employe Number"}= $row['enumber'];
$value{"item"}{$i}{"Name"}= $row['name'];
$value{"item"}{$i}{"Position"}= $row['position'];
$value{"item"}{$i}{"Phone Number"}= $row['phnumber'];
$value{"item"}{$i}{"Location"}= $row['location'];
$i++;
}
// We use JSON.php for convert the data to JSON format and send it to the browser
$json = new Services_JSON();
$output = $json->encode($value);
print($output);
?>
就是上面这些了. 这个脚本的输出如下:
{"item":[{现在让我们使用GWT来实现客户端的用户界面.
"Employe Number":"110009",
"Name":"Juan Hurtado",
"Position":"System Analist",
"Phone Number":"81001121",
"Location":"Monterrey City"}]
}
C:/>projectCreator.cmd -eclipse PHPJSON -out PHPJSON
Created directory PHPJSON/src
Created file PHPJSON/.project
Created file PHPJSON/.classpath
然后我们使用applicationCreator.cmd 建立一个基于eclipse的新的应用程序
C:/>applicationCreator.cmd -eclipse PHPJSON -out PHPJSON com.juan.client.PHPJSON
Created directory PHPJSON/src/com/juan
Created directory PHPJSON/src/com/juan/client
Created directory PHPJSON/src/com/juan/public
Created file PHPJSON/src/com/juan/PHPJSON.gwt.xml
Created file PHPJSON/src/com/juan/public/PHPJSON.html
Created file PHPJSON/src/com/juan/client/PHPJSON.java
Created file PHPJSON/PHPJSON.launch
Created file PHPJSON/PHPJSON-shell.cmd
Created file PHPJSON/PHPJSON-
compile.cmd
public Widget initializeMainForm() {
/*
* Here we initialize and setup a panel for use it as container for the search form and
* the results.
*/
FocusPanel fpn = new FocusPanel();
Grid gd = new Grid(1,2);
b1.setText("Search");
b1.addClickListener(new SearchButtonClickListener());
gd.setWidget(0, 0, txtBox);
gd.setWidget(0, 1, b1);
gdOut.setWidget(0,0,gd);
gdOut.setBorderWidth(1);
gdOut.setWidth("500px");
childGrid.setCellPadding(0);
childGrid.setCellSpacing(0);
childGrid.setWidth("490px");
fpn.add(gdOut);
return fpn;
}
正如你看到的,一个Focus Panel被调用,然后我们在Panel中加入Grid,这个Grid包含所有要用的组件,当用户点击按钮时我们调用SearchButtonClickListener类,代码如下:
private class SearchButtonClickListener implements ClickListener {
/*
* (non-Javadoc)
* @see com.google.gwt.user.client.ui.ClickListener#
* onClick(com.google.gwt.user.client.ui.Widget)
*/
public void onClick(Widget sender) {
/*
* When the user click the button we fetch the URL.
*/
itemNumber = 0;
doFetchURL();
}
private void doFetchURL() {
/*
* Here we fetch the URL and call the handler
*/
b1.setText("Searching ...");
if (!HTTPRequest.asyncGet(DEFAULT_SEARCH_URL + "?tosearch=" +
txtBox.getText(),
new JSONResponseTextHandler())) {
b1.setText("Search");
}
}
}
这个类包含了一个doFetchURL()方法,该方法获取URL地址然后调用JSONResponseTextHandler()类,最后我们开始处理JSON文档.
private class JSONResponseTextHandler implements ResponseTextHandler {
/*
* (non-Javadoc)
* @see com.google.gwt.user.client.ResponseTextHandler#onCompletion(java.lang.String)
*/
public void onCompletion(String responseText) {
/*
* When the fetch has been completed we parse the JSON response and
* display the result
*/
JSONObject jsonObject;
try {
jsonObject = JSONParser.parse(responseText);
displayJSONObject(jsonObject);
} catch (JSONException e) {
}
b1.setText("Search");
}
private void displayJSONObject(JSONObject jsonObject) {
/*
* Here we clear the grid and fill it with the new values.
*/
childGrid.clear();
requestChildrenGrid(jsonObject);
gdOut.setWidget(1,0,childGrid);
}
private void requestChildrenGrid(JSONValue jsonValue){
/*
* Here we fill the grid.
*/
JSONObject jsonObject;
if(jsonValue.isArray() != null){
for(int i = 0; i < jsonValue.isArray().size();i++){
requestChildrenGrid(jsonValue.isArray().get(i));
childGrid.setWidget(itemNumber,0,new HTML("
"));
childGrid.setWidget(itemNumber,1,new HTML("
"));
itemNumber++;
int resizeNumber = itemNumber + 1;
childGrid.resize(resizeNumber,2);
}
} else {
if ((jsonObject = jsonValue.isObject()) != null) {
String[] keys = jsonObject.getKeys();
for (int i = 0; i <
keys.length; ++i) {
String key = keys[i];
childGrid.setWidget(itemNumber,0,new HTML(""+ key +":"));
childGrid.setWidget(itemNumber,1,new HTML(
jsonObject.get(key).toString()));
requestChildrenGrid(jsonObject.get(key));
itemNumber++;
int resizeNumber = itemNumber + 1;
childGrid.resize(resizeNumber,2);
}
} else if (jsonValue != null) {
// Only JSONObject, and JSONArray do anything special with toString, so
// it is safe to handle all non-null cases by simply using toString
//
} else {
//
}
}
}
}
你会发现所有这些方法都在Google例程 JSON 远程过程调用 中的JSON.java文件中,正如你看到的,JSON文档在方法requestChildrenGrid(JSONValue jsonValue)中被分为索引和值,然后格式化索引为粗体并放到表格里,然后放到放到面板中. 最后我们编辑PHPJSON.java文件并且调用 JSONRequester以将结果放到根面板中
/**
* This is the entry point method.
*/
public void onModuleLoad() {
/*
* Just for fun we use a TabPanel as layout
*/
TabPanel tp = new TabPanel();
JSONRequester myJson = new JSONRequester();
tp.add(myJson.initializeMainForm() ,"Corporate Directory");
tp.selectTab(0);
RootPanel.get().add(tp);
}
就到这里, 最后的程序界面如下:
你可以到这里下载所有代码,希望这个小例子对你有用。
____________
Juan Hurtado