In the PHP, I do something like this:
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
$data["result"] = $result_array;
die(urlencode(json_encode($data)));
Then in the GWT:
final String decodedText = URL.decodeComponent(responseText);
try
{
data = JSONParser.parse(decodedText).isObject();
}
catch (final Exception e)
{
// Error code here
}
result = data.get("result").isArray();
final int count = result.size();
for(int i = 0; i < count; i++)
{
final JSONObject o = result.get(i).isObject();
// Process individual object (i.e. one database result line)
here
}
Ian
http://examples.roughian.com
2009/6/10 ANDRES BRUN <[email protected]>
> Hi Ian
> Thank you for your answer, I think that I'm not walk for the wrong way but
> haven't the results that I hope, this is my code:
>
> PHP
> <?php
> //Incluir JSON.php
> require_once("JSON.php");
>
> //Consulto la Base de Datos MySQL y obtengo los resultados
>
> $conector = mysql_connect('localhost','root','doyan2009') or
> die(mysql_error());
> mysql_select_db('music_princess') or die(mysql_error());
>
> $sqlQuery = "select * from tracks order by artista";
>
> $dataReturned = mysql_query($sqlQuery) or die(mysql_error());
>
> $i = 0;
>
> while($row = mysql_fetch_array($dataReturned)){
>
> $value{"item"}{$i}{"ruta_archivo"}=$row['ruta_archivo'];
> $value{"item"}{$i}{"artista"}=$row['artista'];
> $value{"item"}{$i}{"track"}=$row['track'];
> $i++;
> }
>
> $json = new Services_JSON();
> $output = $json->encode($value);
> print($output);
>
> ?>
>
> GWT
>
> import com.google.gwt.user.client.HTTPRequest;
> import com.google.gwt.user.client.ResponseTextHandler;
> import com.google.gwt.user.client.ui.Grid;
> import com.google.gwt.user.client.ui.TextBox;
> import com.google.gwt.user.client.ui.Label;
> import com.google.gwt.user.client.ui.Button;
> import com.google.gwt.user.client.ui.FocusPanel;
> import com.google.gwt.user.client.ui.HTML;
> import com.google.gwt.user.client.ui.ClickListener;
> import com.google.gwt.user.client.ui.Widget;
>
>
>
>
>
> public class PSEscucharMusica {
>
> /*
> * The URL where we go to do the search.
> */
> private String DEFAULT_SEARCH_URL = "
> http://localhost:90/GWT/PSMusic/CargarLista.php";
>
> /*
> * Some widgets that we go to need.
> */
>
> private Button b1 = new Button();
> private Grid gdOut = new Grid(2,1);
> private Grid childGrid = new Grid(5,2);
>
> private int itemNumber = 0;
>
>
> 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(3,2);
>
> b1.setText("Cargar Lista");
> b1.addClickListener(new SearchButtonClickListener());
> gd.setWidget(2, 0, b1);
>
> gdOut.setWidget(0,0,gd);
>
> gdOut.setBorderWidth(1);
> gdOut.setWidth("280px");
>
> childGrid.setCellPadding(0);
> childGrid.setCellSpacing(0);
> childGrid.setWidth("490px");
>
> fpn.add(gdOut);
>
> return fpn;
> }
>
>
> 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("Cargando ...");
> if (!HTTPRequest.asyncGet(DEFAULT_SEARCH_URL, new
> JSONResponseTextHandler())) {
>
> b1.setText("Search");
> }
> }
> }
>
> 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("Cargar");
>
> }
>
>
>
> 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("<HR/>"));
> childGrid.setWidget(itemNumber,1,new HTML("<HR/>"));
>
>
> itemNumber++;
> int resizeNumber = itemNumber + 1;
> childGrid.resize(resizeNumber,2);
> }
> } else {
>
> if ((jsonObject = jsonValue.isObject()) != null) {
>
> String[] keys = jsonObject.getKeys();
> int valida = 0;
>
> for (int i = 0; i < keys.length; ++i) {
> String key = keys[i];
> childGrid.setWidget(itemNumber,0,new HTML("<B>"+ key +":</B>"));
> //Nombre Campo
> childGrid.setWidget(itemNumber,1,new HTML("<a
> href="+jsonObject.get(key).toString()+">"+jsonObject.get(key).toString()+
> "</a>" )); //Valor Campo
>
> requestChildrenGrid(jsonObject.get(key));
>
> itemNumber++;
> int resizeNumber = itemNumber + 1;
> childGrid.resize(resizeNumber,2);
> valida = 1;
> }/*
> if(valida != 0){
> PSMenu mn = new PSMenu();
> mn.presentarVentana();
> }*/
>
> } 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 {
> //
> }
>
>
>
> }
>
>
> }
>
> }
>
> }
>
> That works, but I can't manage the JSON result. I want take one to one the
> results of the PHP but I can't.
>
> I hope that your help me.
>
> Thank you again.
>
> Regards
>
> Andrés
>
>
> On Sat, Jun 6, 2009 at 8:35 AM, Ian Bambury <[email protected]> wrote:
>
>> Hi Andrés,
>>
>> You need to send a request from your GWT app to the PHP script via a
>> requestbuilder
>>
>>
>> final RequestBuilder builder = new
>> RequestBuilder(RequestBuilder.POST, url);
>> builder.setHeader("Content-Type",
>> "application/x-www-form-urlencoded");
>> try
>> {
>> builder.sendRequest(parms, callback);
>> }
>> catch (final RequestException e)
>> {
>> submissionError(url, e);
>> }
>>
>> In your PHP code, return the database results as JSON
>>
>> die(urlencode(json_encode($data)));
>>
>> Back in the GWT app, in the callback routine, extract the data. There are
>> loads of checks you might like to do before this (check for a valid return
>> and not a PHP script error, for example).
>>
>>
>> final String decodedText = URL.decodeComponent(responseText);
>>
>> try
>> {
>> data = JSONParser.parse(decodedText).isObject();
>> }
>> catch (final Exception e)
>> {
>> // Error code here
>> }
>>
>> And then check that you got back what you expected. In the data, you might
>> like to always return a status and check if the script completed
>> successfully. Mine include
>>
>> success - completed correctly, no message
>> message - completed correctly with a message for the user
>> warning - script did not complete as expected - message for the user (e.g.
>> search returned no results, login failed, etc)
>> error - script caught a *real*error - SQL error etc.
>>
>> but I have others
>>
>> Ian
>>
>> http://examples.roughian.com
>>
>>
>>
>> 2009/6/5 ANDRES BRUN <[email protected]>:
>>
>> > Hello Everybody
>> > I'm trying to implement a code that permit me integrate PHP, GWT, MYSQL,
>> I
>> > was testing and implementing different examples but I don't understand
>> > really good, which is the correct way to send data with JSON and
>> > principally. How I can receive that data in GWT? Please, I'm not newbie
>> in
>> > Programation but I'm really newbie in GWT, and this version 1.6 is
>> really
>> > difficult to learn. Somebody can help me with a good example "for
>> > dummies"... jajaja.
>> > Thank you
>> > --
>> > Andrés Brun
>> >
>> >
>> >
>> >
>>
>>
>
>
> --
> ANDRES BRUN
> ------------------------------------------------------------
> WebSite Andres Brun http://www.andresbrun.tk
> Blog - http://doyan2007.blogspot.com/
> WebSite http://www.prolinetsystems.tk
> GWT - http://groups.google.com/group/Google-Web-Toolkit?hl=en
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---