flex與java通訊三:httpService

來源:互聯網
上載者:User

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參數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.