2015-04-14 87 views
0

我想使用HTTP服務連接flex和java。從flex發送到java的參數

<?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" 
bcreationComplete="initApp();"> 
<fx:Script> 
<![CDATA[ 
import mx.controls.Alert; 

private function initApp():void { 
b1.addEventListener(MouseEvent.CLICK, myEventHandler); 
} 

private function myEventHandler(event:Event):void { 
Alert.show("An event occurred."+t1.text); 

srv.send("abc"); 


} 
]]> 
</fx:Script> 

<fx:Declarations> 
<!-- Place non-visual elements (e.g., services, value objects) here --> 
<s:HTTPService id="srv" url="http://localhost:8080/JavaFlex/rest/flextest"> 

</s:HTTPService> 
</fx:Declarations> 
<s:Label> 
Ur message 

</s:Label> 
<s:TextInput id="t1"/> 
<s:Button id="b1" label="Submit" x="120" y="50"> 
</s:Button> 
<mx:DataGrid x="220" y="150" dataProvider="{srv.lastResult}"/> 
</s:Application> 

我打電話給休息服務。 (find all)方法被調用,但是「msg」參數沒有被傳遞。這是我的休息服務。在控制檯中顯示

@Path("/flextest") 
public class chartServer { 

    @GET 
    @Produces({"text/plain"})  
    public String findAll(String msg) throws SQLException { 
    ArrayList<Integer> temp=new ArrayList<Integer>(); 
    System.out.println("op is "+ msg); 
    System.out.println("Rest Api invoked"); 
    chartData r= new chartData(); 
    temp= (ArrayList<Integer>) r.data(); 
    System.out.println("From rest "+temp); 
    return msg; 
    } 

} 

輸出是 op is

爲什麼不顯示參數?

回答

1

的問題是在這條線:

srv.send("abc");

按照HTTPService documentation,你需要的名字 - 值對的對象傳遞給send()。因此,您需要的是:

var values:Object = {}; 
values["msg"] = "abc"; 
srv.send(values); 
+0

謝謝。如何訪問java中的值 – Niketa

1

有兩種方法可將Flex與Java服務應用程序連接:HTTPServices或RemoteObjects。 在你的情況下,這是如何做到:

<?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" 
bcreationComplete="initApp();"> 
<fx:Script> 
<![CDATA[ 
import mx.controls.Alert; 

private function initApp():void { 
b1.addEventListener(MouseEvent.CLICK, myEventHandler); 
} 

private function myEventHandler(event:Event):void { 
// you can define object to send to server here in as code 
// or in mxml httpService component in request tag 
srv.send(); 

} 

private function resultHandler(event:Event):void { 

    datagrid.dataProvider = event.result.yourServicesList; 
} 

private function faultHandler(event:Event):void { 

    Alert.show("some problem :"+event.message); 
} 

]]> 
</fx:Script> 

<fx:Declarations> 
<!-- Place non-visual elements (e.g., services, value objects) here --> 
<s:HTTPService id="srv" url="http://localhost:8080/JavaFlex/rest/flextest" 
      useProxy="false" method="GET"> 
    <s:request xmlns=""> 
     <msg>{t1.text}</msg> 
    </s:request> 
    fault="faultHandler(event);" 
    result="resultHandler(event)" 
</s:HTTPService> 

</fx:Declarations> 
<s:Label> 
Ur message 

</s:Label> 
<s:TextInput id="t1"/> 
<s:Button id="b1" label="Submit" x="120" y="50"> 
</s:Button> 
<mx:DataGrid id = "datagrid" x="220" y="150" /> 
</s:Application> 

您的Java服務應該是這樣的,對於客戶端 收到與@QueryParam註釋對象:

@Path("/flextest") 
public class chartServer { 

    @GET 
    @Produces({"text/plain"})  
    public String findAll(@QueryParam("msg") String msg) throws SQLException { 
    ArrayList<Integer> temp=new ArrayList<Integer>(); 
    System.out.println("op is "+ msg); 
    System.out.println("Rest Api invoked"); 
    chartData r= new chartData(); 
    temp= (ArrayList<Integer>) r.data(); 
    System.out.println("From rest "+temp); 
    return msg; 
    } 

}