首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

使用 GWT 实现基于 Ajax 的 Web 开发(4)远程调用的数据载体

使用 GWT 实现基于 Ajax 的 Web 开发(4)远程调用的数据载体

远程调用的数据载体远程调用时,客户端和服务器端往往要传递大量的信息,这就需要一种能包含各种数据的数据载体,比如 JSON 或 XML。当然也可以是用序列化的 Java 对象,但是对于夸应用的数据传输推荐使用公用的载体:JSON 或 XML。 以下简要介绍在 GWT 中如何使用 JSON 和 XML 进行数据的传输。
使用 JSON 传输数据JSON 的全称是 JavaScript Object Notation,是一种轻量级的数据交换格式。JSON 与 XML 具有相同的特性,例如易于人工编写和阅读,易于机器生成和解析。但是采用 JSON 的数据传输比采用 XML 的数据传输具有更高的有效性。JSON 完全独立于编程语言,并且使用文本格式保存。关于 JSON 更多的了解请阅读 。
在 GWT 中使用 JSON 作为数据传输格式的配置步骤如下:
1)在模块配置文件 .gwt.xml 中添加对 HTTP 和 JSON 的引用。
在 <inherits name='com.google.gwt.user.User'/> 之后添加
<inherits name="com.google.gwt.json.JSON"/>
2)在客户端 / 服务器端建立 JSON 数据并将其作为 RPC 的参数传输到另一端。
清单 6. 在客户端建立 JSON 数据
1
2
3
4
5
6
7
8
9
10
import com.google.gwt.json.client.*

JSONObject jsonObject = new JSONObject();
JSONString value = new JSONString("yuexiaopeng");
jsonObject.put("name", value);

JSONArray arrayValue = new JSONArray();
arrayValue.set(0, new JSONString("item0"));
arrayValue.set(1, new JSONString("item1"));
jsonObject.put("array", arrayValue);




本例中建立的 JSON 数据如下:
1
{name: "yuexiaopeng",  array: {"item", "item"}}




JSON 对象建立后,调用 JSONObject 的 toString() 方法将其转换为 String 做为 RPC 的一个参数,即可传到服务端。
3)在服务器 / 客户端解析 JSON 格式的数据。
清单 7. 在服务器端解析客户端传送的 JSON 数据
1
2
3
4
5
6
7
8
9
10
import org.json.*

JSONObject jsonObject = new JSONObject(jsonBody);
String name = jsonObject.getString("name");
System.out.println("name is:" + name);
JSONArray jsonArray = jsonObject.getJSONArray("array");
for (int i = 0; i < jsonArray.length(); i++)
{
  System.out.println("item " + i + " :" + jsonArray.getString(i));
}




这里 jsonBody 为服务器端从客户端接收到的包含 JSON 信息的字符参数。当然服务端也可根据需要建立 JSON 对象返回给客户端,然后客户端再做解析。
使用 XML 传输数据XML(eXtensible Markup Language)为可扩展的标记语言,是一套定义语义标记的规则。用户可以根据实际需要定义自己的新的标记语言,并为这个标记语言规定其特有的一套标签。XML 易于阅读和编写,更重要的是其可以跨平台跨语言交换数据,使其成为在不同的应用间交换数据的理想格式。关于 XML 更多的了解请阅读 。
在 GWT 中使用 XML 作为数据传输格式,其本质与使用 JSON 一样,需要以下几步 :
1)在模块配置文件 .gwt.xml 中添加对 HTTP 和 XML 的引用。
在 <inherits name='com.google.gwt.user.User'/> 之后添加:
<inherits name ="com.google.gwt.xml.XML"/>
2)在客户端 / 服务器端建立 XML 数据并作为 RPC 的参数传输到另一端。
清单 8. 在服务器端建立 XML 数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import org.w3c.dom.*

Document xmlDoc = new DocumentImpl();
// create the root element
Element rootElement = xmlDoc.createElement("employees");
Element employeeItem = xmlDoc.createElement("employee");
employeeItem.setAttribute("ID", "0008");
Element Name = xmlDoc.createElement("name");
Name.appendChild(xmlDoc.createTextNode("yuexiaopeng"));
employeeItem.appendChild(Name);
rootElement.appendChild(employeeItem);
xmlDoc.appendChild(rootElement);

//convert XML object to string
Document document = rootElement.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
String result = serializer.writeToString(document);




清单 9. 本例中建立的 XML 数据
1
2
3
4
5
6
7
<employees>
    <employee ID = "0008">
        <name>
            yuexiaopeng
        </name>
    </employee>
</employees>




3)在服务器 / 客户端解析 XML 格式的数据。
清单 10. 客户端解析接收到的 XML 数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import com.google.gwt.xml.client.*

Document employeeDom = XMLParser.parse(xmlText);
Element employeeElement = employeeDom.getDocumentElement();
XMLParser.removeWhitespace(employeeElement);
NodeList employees = employeeElement.getElementsByTagName("employee");
Element employee = (Element) employees.item(0);

NamedNodeMap attributes = employee.getAttributes();
Node attribute = attributes.getNamedItem("ID");
String ID = attribute.getNodeValue();

String name = employee.getElementsByTagName("name").item(0).getFirstChild()
                .getNodeValue();




这里 xmlText 为服务器端从客户端接收到的包含 xml 信息的字符串参数。当然客户端也可先建立 XML 对象再提交给服务器端,然后服务器端再做解析。
返回列表