Flex HttpService获取服务端返回数据 xml object text等

语言版本: ActionScript 3.0
产品版本: Flex 3
运行时版本: Flash Player 9, AIR 1.1

使用 HTTPService 类可表示 ActionScript 中的 HTTPService 对象。当调用 HTTPService 对象的 send() 方法时,将发出对指定 URL 的 HTTP 请求,并且返回 HTTP 响应。可以选择向指定 URL 传递参数。如果没有使用基于服务器的代理服务,则只能使用 HTTP GET 或 POST 方法。如果将 useProxy 属性设置为 true 并使用基于服务器的代理服务,则还可以使用 HTTP HEAD、OPTIONS、TRACE 和 DELETE 方法。 


该对象可指定服务器返回数据到httpservice后解析数据的类型,通过设置resultFormat

resultFormat

属性  

resultFormat:String

语言版本: ActionScript 3.0
产品版本: Flex 3
运行时版本: Flash Player 9, AIR 1.1

指示如何反序列化由 HTTP 调用返回的结果的值。该项目的值根据以下条件确定:

  • 返回的是 XML 还是名称/值对。
  • 访问结果的方式;可以将结果作为 object、text 或 XML 进行访问。

默认值为 object。允许使用的值包括:

  • object 返回的值为 XML 并且按照 ActionScript 对象树分析。此为默认。
  • array 返回的值是 XML 并且按照 ActionScript 对象树分析。但是,如果顶级对象不是数组,将创建一个新数组并且将结果设置为第一个项目。如果 makeObjectsBindable 为 true,则该数组将被包装在 ArrayCollection 中。
  • xml 返回的值为 XML 并且作为 ActionScript XMLnode 对象中的文本 XML 返回。
  • flashvars 返回的值是包含由 & 符号分隔的名称=值对的文本,该文本被分析为 ActionScript 对象。
  • text 返回的值为文本并且未经处理。
  • e4x 返回的值为 XML 并且作为 ActionScript XML 对象中的文本 XML 返回,可以使用 ECMAScript for XML (E4X) 表达式进行访问。 
如服务端使用servlet读取一个xml配置文件并返回给httpservice的例子:

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, HttpServletResponse response)
			throws ServletException, 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>



你可能感兴趣的:(xml,object,String,Flex,application,actionscript)