Getting Flex 3 talking to Java via JSON
packagecom.giantflyingsaucer;
importjava.io.*;
importjava.io.PrintWriter;
importjavax.servlet.*;
importjavax.servlet.http.*;
importorg.json.simple.*;
publicclass MessageServlet extendsHttpServlet
{
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException
{
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
JSONObject jsonObject = newJSONObject();
try
{
String strJSON = null;
JSONObject myObject = null;
if(request.getParameter("myObject") != null)
{
strJSON = request.getParameter("myObject");
Object obj = JSONValue.parse(strJSON);
myObject = (JSONObject)obj;
String value1 = myObject.get("value1").toString();
String value2 = myObject.get("value2").toString();
jsonObject.put("value1","Hello: " + value1);
jsonObject.put("value2","Hello: " + value2);
}
else
{
jsonObject.put("ERROR","Invalid JSON");
}
}
finally
{
out.print(jsonObject);
out.flush();
out.close();
}
}
}
Here is a quick and easy way to pass JSON to and from Flex 3 and a Java Servlet. First off you need to grab the AS3CoreLib from Google
Code. Once you have the as3corelib you’ll want to make it so you can use the contents of “corelib/src” from within your Flex project. I imported it into Flex Builder 3 (beta 2). For the Java part of it you can use whatever you like, I like to use Eclipse with
the MyEclipseIDEplugin and then use their “Web Project” wizard
to get my web app going. I used Tomcat 6 for the servlet container and added a single servlet to the project.
For the Java servlet to understand the JSON I grabbed the “Simple JSON” library here.
Its very easy to use, just import it into your Eclipse web project project (or whatever your using). Here is the servlet’s code:
Now for the Flex code:
<?xml version="1.0"encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute">
<mx:Script>
<![CDATA[
importcom.adobe.serialization.json.JSON;
importflash.events.*;
importflash.net.*;
publicfunction runMe():void
{
varloader:URLLoader = newURLLoader();
varheader1:URLRequestHeader = null;
varrequester:URLRequest = newURLRequest();
header1 = newURLRequestHeader("pragma","no-cache");
requester.requestHeaders.push(header1);
requester.method = URLRequestMethod.POST;
configureListeners(loader);
loader.dataFormat = URLLoaderDataFormat.TEXT;
// Modify to your needs
requester.url = "http://localhost:8085/MessageServer/servlet/MessageServlet";
varobj:Object= newObject();
obj.value1 = "Bob";
obj.value2 = "Sandy";
varvariables:URLVariables = newURLVariables();
variables.myObject = JSON.encode(obj);
requester.data = variables;
loader.load(requester);
}
privatefunction configureListeners(dispatcher:IEventDispatcher):void
{
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
dispatcher.addEventListener(Event.OPEN, openHandler);
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
privatefunction completeHandler(event:Event):void
{
try
{
vartempLoader:URLLoader = URLLoader(event.target);
varobj:Object= JSON.decode(tempLoader.data);
trace("completeHandler: " + tempLoader.data);
trace(obj.value1);
trace(obj.value2);
}
catch(error:Error)
{
trace("completeHandler: " + error.toString());
}
}
privatefunction openHandler(event:Event):void
{
trace("openHandler: " + event);
}
privatefunction progressHandler(event:ProgressEvent):void
{
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
privatefunction securityErrorHandler(event:SecurityErrorEvent):void
{
trace("securityErrorHandler: " + event);
}
privatefunction httpStatusHandler(event:HTTPStatusEvent):void
{
trace("httpStatusHandler: " + event);
}
privatefunction ioErrorHandler(event:IOErrorEvent):void
{
trace("ioErrorHandler: " + event);
}
]]>
</mx:Script>
<mx:Button x="19"y="10"label="Run Me" id="btnRunMe"click="runMe()"enabled="true"/>
</mx:Application>
You should see something like this:
openHandler:progressHandlerhttpStatusHandler:completeHandler:Hello:Hello: |
I also like to use Fiddler 2 to inspect whats going over
the wire. Firebug for Firefox I assume
would work as well. These tools can be very handy when you want to inspect whats going on – especially when you need to debug an issue.
Getting Flex 3 talking to Java via JSON的更多相关文章
- Flex使用Blazeds与Java交互及自定义对象转换详解-DATAGRID读取ORACLE数据
http://www.cnblogs.com/RocD-DuPeng/articles/1751040.html 一.建立Flex与Java交互的工程. 本文中讲到的交互是利用Blazeds的,因为这 ...
- Java集合 Json集合之间的转换
1. Java集合转换成Json集合 关键类:JSONArray jsonArray = JSONArray.fromObject(Object obj); 使用说明:将Java集合对象直接传进JSO ...
- Java对象 json之间的转换(json-lib)
在这里主要简单的介绍一下,如何使用json-lib这个工具包来完成Java对象(或集合)与json对象(或集合)之间的转换~ 1. Java对象转换成json(既创建json) 关键类:JSONObj ...
- Java 的 JSON 开源类库选择比较(zz)
在看了作者的介绍,然后我又到mvnrepository上去看了各个库的的使用数之后,发现只能在jackson和gson之间做选择. 以下是原文 有效选择七个关于Java的JSON开源类库 April ...
- java中json包的使用以及字符串,map,list,自定义对象之间的相互转换
做一个map和字符串的转换,需要导入这些jar包,这是最基本的一些jar包. 经过多方尝试得出结论入下: 首先导入基本包:json-lib-2.2.3-jdk15.jar 如果没有这个jar包,程序是 ...
- java系列--JSON数据的处理
http://blog.csdn.net/qh_java/article/details/38610599 http://www.cnblogs.com/lanxuezaipiao/archive/2 ...
- Java之JSON数据
特别注意:使用JSON前需要导包 操作步骤地址:http://blog.csdn.net/baidu_37107022/article/details/70876993 1.定义 JSON(JavaS ...
- JSON以及Java转换JSON的方法(前后端常用处理方法)
)); map.put("arr", new String[] { "a", "b" }); map.put("func" ...
- java处理json与对象的转化 递归
整个类是一个case,总结了我在使用java处理json的时候遇到的问题,还有级联关系的对象如何遍历,json和对象之间的转换! 对于对象json转换中遇到的问题我参考了一篇博客,http://blo ...
- Java JWT: JSON Web Token
Java JWT: JSON Web Token for Java and Android JJWT aims to be the easiest to use and understand libr ...
随机推荐
- Docker网络上篇-网络介绍
通过前面的学习,我们已经可以把自己写的微服务项目通过dockerfile文件方式部署到docker上面了.那么微服务之间通信,怎么通信的?是在同一个网络还是在不同的网络环境下?docker中怎么配置网 ...
- Postman Code Java-Unirest 代码的依赖
本来是Postman的Code直接使用的,结果根据这个名字 Unirest,搜出来了很多依赖,使用了排名第一的, https://search.maven.org/search?q=Unirest 结 ...
- ptmalloc2涉及的基础知识与基本数据结构
随笔来源:ctfwiki CSDN 本随笔只为记录分析总结的自己学习的结论,方便未来回顾,以及为他人提供一个理解的思路,不保证正确.如有谬误,请大家指出. 1.堆相关的操作 malloc:返回对应大小 ...
- 知识点考古:php5的面向对象学习笔记
闲来无事翻看以前收藏的资料,考古到保存的这篇文章对php的OOP的整理还很系统.原链接已经打不开(http://www.cublog.cn/u/17686/showart.php?id=146562) ...
- WPF 实现一个吃豆豆的Loading加载动画
运行的效果如下 先引入一下我们需要的库 在nuget上面搜一下"expression.Drawing",安装一下这个包 我们再创建一个Window,引入一下这个包的命名空间 我们设 ...
- ASP.NET Core – Razor Pages Routing
前言 之前有提过, MVC 和 Razor Pages 最大的区别就在 Routing 上. Razor Pages 的结构是 route, page, model route match to pa ...
- C++ STL queue容器——队列
queue容器 基本概念 queue是一种**先进先出的数据结构,它有两个出口,queue容器允许从一端新增元素,从另一端移除元素. queue容器没有迭代器,所有元素进出都必须符合"先进先 ...
- Java中使用BigDecimal进行double类型的计算(高精度,可保留几位小数)
Java中 小数直接进行乘除运算,会出现精度问题导致计算结果有误需要使用 BigDecimal 类型辅助运算,保证精度无误源码: import java.math.BigDecimal;import ...
- 线段树can you answer these queries-------hdu4027
问题描述: 给定一个数列,要求对指定区间内所有数开方,输出查询区间和 输入: 有很多个测试用例,每个用例第一行输出一个整数N,表示数列有N个数,1<=N<=100000;第二行输入N个整数 ...
- [TK] 三角蛋糕 hzoi-tg#261
同机房大佬也写了这道题的 题解. 我在另一篇 题解 中提到了这类问题的通解,接下来我们依照此通解思考该题. 问题处理 首先我们来定义三角形的表示方式. 定义 \(f[i][j]\) 表示三角形 \(( ...