0 Replies Latest reply on Jun 14, 2010 7:28 AM by Sen Imagines

    Sending JSON format data with <mx:HTTPService> parameter

    Sen Imagines Level 1

      Hi,

       

      I wish to send a JSON structured data (string:

       

      {"data1":"ID001","data2":"myItemName","data3":"myItemParameter3","data4":"myItemParameter4 "})

       

      with <mx:HTTPService> using send(Parameter) to a Java function "myDataFunction.action"

      But when I send this kind of string, it is throwing error.

       

      I am creating an Object called: myParams

      To this, I add an attribute: param1

      param1 contains a JSON formatted string.

       

      I wish to pass this kind of string to the Java function.

      Can the Java function able to access this kind of string which is sent from Flex?

       

      Suggest me with the changes to be made so that I can achieve the above.

       

      -  Sen

       

      Below is the code:

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="0xFFFFFF" >

       

      <mx:HTTPService id="myHTTPServ" url="http://MyProjName/myDataFunction.action" resultFormat="e4x" showBusyCursor="true" fault="FaultHandler(event)" result="myResultHandler(event)" />

       

      <mx:Script>

      <![CDATA[

      import mx.controls.Alert;

       

      import mx.rpc.events.FaultEvent;

      import mx.rpc.events.ResultEvent;

       

      // START of Functions to get and handle chart Data for the specific ID

      public function callData():void

      {

      var myitem1:String;

      var myitem2:String;

      var myitem3:String;

      var myitem4:String;

       

      var myitems:String;

       

      myitem1 = "ID001";

      myitem2 = "myItemName";

      myitem3 = "myItemParameter3";

      myitem4 = "myItemParameter4";

       

       

      var myParams:Object = new Object();// Create a parameter object for passing to the HTTPService

       

      //Data Structure to be sent as parameter:

      //{"data1":"ID001","data2":"myItemName","data3":"myItemParameter3","data4":"myItemParamete r4"}

       

      myitems = "{data1:"+myitem1+",data2:"+myitem2+",data3:"+myitem3+",data4:"+myitem4+"}";

       

      myParams.param1 = myitems;

       

      Alert.show(myParams.param1);

       

      callHTTPReq(myParams);

      }

       

      public function callHTTPReq(myparams:Object):void

      {

      myHTTPServ.send(myparams); // Call the HTTPService by passing the parameters

      }     

       

         private function myResultHandler(event:ResultEvent):void

         {

      //Handling the received XML data

       

         } 

       

       

      // START of Common Fault Handler

              private function FaultHandler(event:FaultEvent):void

              {

              Alert.show(event.fault.message);

              }

      // END of Common Fault Handler

       

      ]]>

      </mx:Script>

      <mx:HBox width="100%">

      <mx:Button label="Home" click="callData()"/>

      </mx:HBox>

      </mx:Application>