Question

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...

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top