2009-07-10 48 views
1

我在GWT(1.6)中使用了一個RequestBuilder,它成功地將字符串(格式化爲日期)發送到我的Web服務器上的PHP腳本。然後,我的PHP使用該字符串來查詢MySQL數據庫。然後我能夠迴應一個由GWT成功解釋的結果。將數組從PHP發送到GWT的問題

我的問題是,我不只是想回顯一個字符串。我想發送一個數組回到GWT。這裏的問題是GWT得到一個'Response'類型的對象,而不是Array。

有誰知道我能做什麼?下面是一些代碼:

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 ([email protected]_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代碼(片段):

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 

} 

我知道,我可以使用JSON,但我試過了,無法得到它的工作。有任何想法嗎?

謝謝...

回答

1

echo "$array";只會輸出 '數組' 或類似的。您可以看看發送JSON或XML,例如使用json_encode(),例如,

echo json_encode($array); 

我會想象你可以在GWT很容易分析這個 - 見JSONParser

+0

在我看來,該JSONParser只能解析字符串轉換成JSON值......我錯了這個? – littleK 2009-07-10 16:15:39