views:

234

answers:

1

Hey everyone,

I am using a RequestBuilder in GWT (1.6) that successfully sends a String (formatted as a date) to a PHP script on my web server. My PHP then uses that String to query a MySQL Database. I am then able to echo a result, which is successfully interpreted by GWT.

My issue is that I don't just want to "echo" back a String. I want to send an Array back to GWT. The issue here is that GWT gets an object of type 'Response', and not Array.

Does anyone know what I can do? Here is some code:

 PHP CODE:

 <?php 
require_once('../scripts/config.php');

$date = $GLOBALS['HTTP_RAW_POST_DATA']; 

$query = mysql_query("SELECT * FROM eventcal WHERE eventDate = '$date'");

if (@mysql_num_rows($query)) {
 while ($r=@mysql_fetch_assoc($query)) { 
  $id = $r["id"];  
  $primary = $r["primary"];
     $secondary = $r["secondary"];
     $eventContent = $r["eventContent"];
     $region = $r["region"];

    }
}

$array = array("$id", "$primary", "$secondary", "$eventContent", "$region");

echo "$array";

 ?>

GWT Code (Snippets):

 public void onChange(Widget sender) {
 lb.setText("Date selected: " + calendar.getDate());
 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
 String current = df.format(calendar.getDate());

 RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,   URL.encode("http://www.kbehr.com/calendar/view_duty.php"));

try {
  builder.sendRequest(current, new RequestCallback(){
    public void onError(Request request, Throwable exception) {
   requestFailed(exception);
    }

    public void onResponseReceived(Request request, Response response) {

     String responseText = response.getText();
        try {
          processResults(responseText);
        } catch (Exception e) {
          Window.alert(responseText);
        }

 }});
}catch (RequestException ex) {
  requestFailed(ex);
}    

 }});
 fp.add(calendar);
 fp.add(lb);  

 }

 private void processResults(String something){

  // process the array

 }

I know that I can use JSON, but I tried that and couldn't get it working. Any ideas?

Thanks...

+1  A: 

The line echo "$array"; will just output 'array' or similar. You could look at sending back JSON or XML, for example using json_encode(), e.g.

echo json_encode($array);

I would imagine that you could parse this quite easily in GWT - see JSONParser

Tom Haigh
It seems to me that the JSONParser can only parse a String into a JSON value...am I wrong on this?
behrk2
I think I will try to use XML as you suggested, with this as a guide:http://www.ibm.com/developerworks/opensource/library/x-gwtphp/index.html?ca=drs-
behrk2
Yes, it does parse a string. That is what you want. In PHP you convert your array into a JSON string, and then in GWT you parse that JSON string back into a data structure. I would choose this over XML - it should be simpler.
Tom Haigh
Gotcha. Thanks!
behrk2