在GWT和PHP之间传递数据

时间:2012-05-21 20:22:31

标签: php json gwt

我是GWT和PHP的新手,在阅读了一些教程后,我不清楚如何在GWT前端和PHP后端之间有效地交换数据。我成功地遵循了Google教程,建议使用RequestBuilder类从PHP获取数据。 但是当我需要保存GWT中完成的工作时,如何有效地将数据传递给PHP? 我不清楚如何使用RequestBuilder完成此任务。 我现在找到的解决方案是

RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, URL.encode(url));
    builder.setHeader("Content-Type", "application/json");
        try {
              Request request = builder.sendRequest("{\"done\":false,\"description\":\"Some text\",\"priority\":0}", new RequestCallback() {...

并在PHP方面

$arr = json_decode(file_get_contents("php://input"),true);
$done = (int)$arr['done'];
$description = $arr['description'];
$priority = $arr['priority'];
mysql_query("INSERT INTO Tasks (done, description, priority)
    VALUES ($done, '$description', $priority)");

这是最好的方法吗?有人在网络上找到了一个有效的例子吗?我们欢迎每一个意见......

1 个答案:

答案 0 :(得分:0)

我认为,在与非Java服务器交互时使用JSON很好。您可以使用JSON模块以更安全的方式在客户端构建JSON字符串:

在.gwt.xml文件中添加

<inherits name='com.google.gwt.json.JSON'/>

然后你可以构建你的示例字符串,如

final JSONObject jsonObject = new JSONObject();

jsonObject.put("done", JSONBoolean.getInstance(false));
jsonObject.put("description", new JSONString("Some text"));
jsonObject.put("priority", new JSONNumber(0));

final String jsonString = jsonObject.toString();