【问题标题】:Question with sending an array from PHP to GWT将数组从 PHP 发送到 GWT 的问题
【发布时间】:2009-07-10 15:18:54
【问题描述】:

我在 GWT (1.6) 中使用 RequestBuilder,它成功地将字符串(格式化为日期)发送到我的 Web 服务器上的 PHP 脚本。然后我的 PHP 使用该字符串来查询 MySQL 数据库。然后我可以回显一个结果,该结果由 GWT 成功解释。

我的问题是我不只是想“回显”一个字符串。我想将一个数组发送回 GWT。这里的问题是 GWT 得到一个“响应”类型的对象,而不是数组。

有人知道我能做什么吗?这是一些代码:

 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,但我尝试过但无法正常工作。有什么想法吗?

谢谢...

【问题讨论】:

    标签: php mysql gwt httprequest


    【解决方案1】:

    echo "$array"; 行将只输出 'array' 或类似内容。您可以查看发回 JSON 或 XML,例如使用 json_encode(),例如

    echo json_encode($array);
    

    我想你可以在 GWT 中很容易地解析这个 - 请参阅 JSONParser

    【讨论】:

    • 在我看来 JSONParser 只能将 String 解析为 JSON 值...我错了吗?
    • 我想我会按照您的建议尝试使用 XML,以此为指导:ibm.com/developerworks/opensource/library/x-gwtphp/…
    • 是的,它确实解析了一个字符串。这就是你想要的。在 PHP 中,您将数组转换为 JSON 字符串,然后在 GWT 中,您将该 JSON 字符串解析回数据结构。我会选择这个而不是 XML - 它应该更简单。
    猜你喜欢
    • 2016-10-13
    • 2014-09-22
    • 1970-01-01
    • 2016-09-26
    • 2012-12-14
    • 2014-04-09
    • 2017-06-17
    • 1970-01-01
    相关资源
    最近更新 更多