1

GWT(1.6)でRequestBuilderを使用しており、Webサーバー上のPHPスクリプトに文字列(日付としてフォーマットされている)を正常に送信しています。次に、私のPHPはその文字列を使用してMySQLデータベースにクエリを実行します。その後、結果をエコーすることができます。これは、GWTによって正常に解釈されます。

私の問題は、文字列を「エコー」するだけではないということです。アレイをGWTに送り返したいのですが。ここでの問題は、GWTがArrayではなく「Response」タイプのオブジェクトを取得することです。

誰かが私に何ができるか知っていますか?ここにいくつかのコードがあります:

 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コード(スニペット):

 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を使用できることは知っていますが、それを試したところ、機能しませんでした。何か案は?

ありがとう...

4

1 に答える 1

1

echo "$array";は「配列」などを出力するだけです。json_encode()たとえば、JSON または XML を使用して返送することを検討できます。

echo json_encode($array);

これは GWT で非常に簡単に解析できると思います。JSONParserを参照してください。

于 2009-07-10T15:40:27.030 に答える