简体   繁体   中英

Parameters not sent from flex to java

I want to connect flex and java using HTTP services.

<?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>

I am calling a rest service. The (find all) Method is invoked, but the "msg" parameter is not being passed. Here is my Rest Service.

@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;
    }

}

Output displayed in console is op is

Why isn't the parameter displayed?

The problem is in this line:

srv.send("abc");

According to the HTTPService documentation , you need to pass an Object of name-value pairs to send() . Thus, what you need is:

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

There are two ways to connect flex with java services app: HTTPServices or RemoteObjects. In your case this is how to do:

<?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>

your Java service should look like this, the object received from client side with @QueryParam annotation :

@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;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM