flex與java通訊的三種方式:
1.通過httpService:你訪問一個xml或者servlet,然後flex接收返回的xml資料2.通過webService:你訪問一個webService服務,然後獲得文本資訊3.通過RemoteObject:可以通過blazeds來訪問遠程service介面方法,Flex接收一個對象。
建立項目:(來自百度文庫的《flex(eclipse)開發配置手冊.pdf》)
flexHttpService訪問定義在javaFlex裡面的httpservice方法:
1、在javaFlex中定義一個servlet,該servlet返回一個xml
package my.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@SuppressWarnings("serial")public class HttpServiceServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setContentType("text/xml");resp.setCharacterEncoding("utf-8");PrintWriter out = resp.getWriter();out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");out.println("<rows>");out.println("<row>");out.println("<id>");out.println("1");out.println("</id>");out.println("<msg>");out.println("hello");out.println("</msg>");out.println("</row>");out.println("<row>");out.println("<id>");out.println("2");out.println("</id>");out.println("<msg>");out.println("haha");out.println("</msg>");out.println("</row>");out.println("</rows>");out.close();}}
在web.xml中配置servlet:
<servlet> <servlet-name>MyHttpServiceServlet</servlet-name> <servlet-class>my.servlet.HttpServiceServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyHttpServiceServlet</servlet-name> <url-pattern>/MyHttpServiceServlet</url-pattern> </servlet-mapping>
2、在flexHttpService中定義並訪問 MyHttpServiceServlet:
定義:
<fx:Declarations><!-- 將非可視元素(例如服務、值對象)放在此處 --><s:HTTPService id="getData" method="POST" url="http://localhost:8080/javaFlex/MyHttpServiceServlet" useProxy="false"/></fx:Declarations>
也可以不指定url而在proxy-config中配置: 在proxy-config.xml配置HTTPService要請求的URL地址,在程式中用“destination”映射關係來訪問該URL
訪問:
<s:Button label="Get data" click="getData.send()"/>
得到結果:
<mx:DataGrid width="332" height="160" dataProvider="{getData.lastResult.rows.row}">3、結果:
---------------------------------------------------------華麗麗的分割線-------------------------------------------------------------------------------
上面沒有解決跨域訪問的問題:
來自: http://hi.baidu.com/hakerivan/item/211218d6c6a348866cce3f7f (未驗證)
沙箱效應的方法:
1、利用useProxy屬性和crossdomain.xml可以跨域訪問,這是解決之一。
2、寫一個JS或PHP指令碼,用Ajax(XMLHTTP Object) 去訪問跨域URL。
<mx:HTTPService id="srv" destination="catalog" useProxy="true"/>
WEB-INF\flex\proxy-config.xml :
<destination id="catalog"> <properties> <url>/{context.root}/testdrive-httpservice/catalog.jsp</url> </properties> </destination>
其他: flash跨域策略檔案crossdomain.xml配置詳解 HttpService 使用useProxy參數