Flex在做界面开发的时候有很多优势,Flex+java的组合也越来越流行,在整合开发的过程中自然会遇到数据交互的问题,下面介绍Flex3+Java整合使用json的情况。
一、Flex3中使用json需要corelib.swc http://www.adobe.com/cfusion/exchange/index.cfm?view=sn111&extid=1078469
将corelib.swc拷贝到Flex安装目录的sdks\3.0.0\frameworks\libs下
二、Java中进行数据处理得到json格式的数据,部分代码
Java代码
01./**
02. * 浏览器直接返回信息
03. *
04. * @param content 内容
05. * @param character 编码
06. * @throws IOException
07. */
08.protected void out(String content,String character) throws IOException {
09. HttpServletResponse response = ServletActionContext.getResponse();
10. response.setContentType("text/html;charset="+character);
11. PrintWriter out = response.getWriter();
12. out.print(content);
13. out.flush();
14. out.close();
15.}
16.
17. try{
18. Author author = authorService.getAuthorByName(username);
19. if( author==null ){
20. JSONObject json = new JSONObject();
21. json.put("status","0");
22. json.put("message","用户名不存在");
23. super.out(json.toString());
24. }
25. if(author.getPassword().equals(MD5.md5(password))){
26. session.put(Symbols.SESSION_AUTHOR, author);
27. JSONObject json = new JSONObject();
28. json.put("status","1");
29. json.put("message","登录成功");
30. super.out(json.toString());
31. }
32. else{
33. JSONObject json = new JSONObject();
34. json.put("status","0");
35. json.put("message","密码错误");
36. super.out(json.toString());
37. }
39. }
40. catch(ServiceException se){
41. JSONObject json = new JSONObject();
42. json.put("status","0");
43. json.put("message","登录失败");
44. super.out(json.toString());
45. throw new Exception(se);
46. } finally {
47. }
48. return NONE;
三、Flex中请求URL得到json的数据,然后在script中解析json数据
XML/HTML代码
01.<?xml version="1.0" encoding="utf-8"?>
02.<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
03. horizontalAlign="center"
04. verticalAlign="middle"
05. verticalGap="15"
06. horizontalGap="15">
08. <mx:Script>
09. <![CDATA[
10. import mx.collections.ArrayCollection;
11. import mx.rpc.events.ResultEvent;
12. import com.adobe.serialization.json.JSON;
13. import mx.controls.Alert;
14.
15. [Bindable]
16. private var photoFeed:ArrayCollection;
17.
18. private function doLogin():void {
19. trace("start send");
20. var params:Object = new Object();
21. params.username = username.text;
22. params.password = password.text;
23. loginCheck.send(params);
24. trace("end send");
25. }
26.
27. private function doCancel():void {
28. username.text = "";
29. password.text = "";
30. }
31.
32. private function loginHandler(event:ResultEvent):void {
33.
34. var rawData:String = String(event.result);
35.
36. //decode the data to ActionScript using the JSON API
37. //in this case, the JSON data is a serialize Array of Objects.
38. var arr:Array = (JSON.decode(rawData) as Array);
39. var dataArray:ArrayCollection = new ArrayCollection(arr);
40. //arraySize = dataArray.length;
41.
42. // info.text = event.result.toString();
43. var obj:Object = JSON.decode(rawData);
44.
45. if(obj.status==1){
46. var request:URLRequest = new URLRequest();
47. request.method = URLRequestMethod.GET;
48. request.url = "http://test.ntsky.com:8080/admin/posts.action";
49. // request.
50. var loader:URLLoader = new URLLoader();
51. navigateToURL(request,"_self");
52. }
53. else{
54. Alert.show(obj.message);
55. }
56. }
57.
58. ]]>
59. </mx:Script>
61. <mx:HTTPService id="loginCheck"
62. url="http://test.ntsky.com:8080/admin/login.action"
63. resultFormat="text"
65. result="loginHandler(event)" />
67. <mx:Panel x="330" y="192" width="290" height="177" layout="absolute" title="用户登录" fontSize="12" horizontalAlign="center" verticalAlign="middle">
68. <mx:Form x="10" y="10" width="253" height="90">
69. <mx:FormItem label="用户名:">
70. <mx:TextInput id="username"/>
71. </mx:FormItem>
72. <mx:FormItem label="密 码 :">
73. <mx:TextInput id="password" displayAsPassword="true"/>
74. </mx:FormItem>
75. </mx:Form>
76. <mx:Button x="65" y="103" label="登录" click="doLogin()"/>
77. <mx:Button x="144" y="103" label="取消" click="doCancel()"/>
78. </mx:Panel>
80.</mx:Application>
Flex3 HTTPService和Java使用json交互(登录示例)
最新推荐文章于 2024-08-03 23:12:36 发布