Flex HttpService获取服务端返回数据 xml object text等
发布时间:2020-12-15 04:48:36 所属栏目:百科 来源:网络整理
导读:语言版本: ActionScript 3.0 产品版本: Flex 3 运行时版本: Flash Player 9,AIR 1.1 使用 HTTPService 类可表示 ActionScript 中的 HTTPService 对象。当调用 HTTPService 对象的 send() 方法时,将发出对指定 URL 的 HTTP 请求,并且返回 HTTP 响应。可以选
使用 HTTPService 类可表示 ActionScript 中的 HTTPService 对象。当调用 HTTPService 对象的 该对象可指定服务器返回数据到httpservice后解析数据的类型,通过设置resultFormat
resultFormat:String
指示如何反序列化由 HTTP 调用返回的结果的值。该项目的值根据以下条件确定:
默认值为
JAVA ?servlet: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import javax.servlet.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class ConfigService extends HttpServlet implements Servlet { private static final long serialVersionUID = 1L; private static final String defaultCharSet = "utf-8"; private static final String defaultContentType = "xml"; /** * Constructor of the object. */ public ConfigService() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { doPost(request,response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request,IOException { String contentType = RequestUtils.getParameter(request,"contentType"); if (StringUtils.isBlank(contentType)) { contentType = defaultContentType; } response.setContentType("text/" + contentType + ";charset=" + defaultCharSet); response.setCharacterEncoding(defaultCharSet); PrintWriter out = response.getWriter(); String configName = RequestUtils.getParameter(request,"configName"); if (StringUtils.isNotBlank(configName)) { try { Resource resource = new ClassPathResource("config/" + configName + "." + contentType); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(resource.getInputStream(),defaultCharSet),1024 * 512); String line = null; while ((line = bufferedReader.readLine()) != null) { out.println(line); } } catch (Exception e) { e.printStackTrace(); } } out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here } } Flex: <?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)"> <fx:Declarations> <s:HTTPService id="httpService" result="httpService_resultHandler(event)" resultFormat="xml" url="http://127.0.0.1:8080/ws/servlet"> </s:HTTPService> </fx:Declarations> <fx:Script> <![CDATA[ import mx.events.FlexEvent; import mx.rpc.events.ResultEvent; protected function application1_creationCompleteHandler(event:FlexEvent):void { httpService.send(); } protected function httpService_resultHandler(event:ResultEvent):void { var xmlResult :XML = XML(event.result); //xml处理 } ]]> </fx:Script> </s:Application> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |