4 Replies Latest reply on Apr 1, 2008 5:17 PM by meena_r

    Java Webservice and Flex

    meena_r
      Hello All,
      I am stuck at getting webservice result from flex. I have created a webservice which has following method:
      @WebMethod
      public String Login(@WebParam(name = "userID") String userID, @WebParam(name = "password") String password) {
      // TODO Store privilege and user ID in session
      String privilege = UserManager.getInstance().validateUserLoginInfo(userID,password);
      return privilege;
      }
      I was getting correct response from this service(as object format) untill I defined
      @WebResult(name="userPrivilege") in the method. after that I am getting null if I defined Web service defination in flex MXML as follows:
      <mx:WebService id="wsLogin" service="NMSMainServiceService" port="NMSMainServicePort"
      wsdl=" http://localhost:8080/NMS/NMSMainService?wsdl" >
      <mx:operation name="Login" result="loginResultHandler(event)" fault="loginFaultHandler(event)">
      <mx:request>
      <userID>{userID.text}</userID>
      <password>{password.text}</password>
      </mx:request>
      </mx:operation>
      </mx:WebService>

      I don't know any reason, then if I define resultFormat="xml" or "e4x" I do get response by event.result however I can not access a node in the response array or xml.

      private function loginResultHandler(event:ResultEvent):void{
      //var myXML:Array = event.result as Array;
      var can:String = event.result.LoginResponse.userPrivilege;
      Alert.show(can);
      }
      is giving me error saying : A term is undefined. When I checked response in log file it is :
      <ns2:LoginResponse xmlns:ns2=" http://service.nms.satpath.com/" xmlns:S=" http://schemas.xmlsoap.org/soap/envelope/">
      <userPrivilege>manager</userPrivilege>
      </ns2:LoginResponse>

      I have created namespace too
      private var ns2:Namespace = new Namespace(" http://service.nms.satpath.com/");
      private var S:Namespace = new Namespace(" http://schemas.xmlsoap.org/soap/envelope/");

      but no luck.
      I would really appreciate if somone could help me this, please do let me know if you need any more information. Please help, its urgent.
      Thanks.
        • 1. Re: Java Webservice and Flex
          SujitG Level 2
          Hi,
          try this
          set the resultFormat of the operation to "object" and then modify the loginResultHandler as below
          private function loginResultHandler(event:ResultEvent):void{
          var can:String = event.result as String;
          Alert.show(can);
          }

          Hope this helps.
          • 2. Re: Java Webservice and Flex
            meena_r Level 1
            Thanks for your response Sujit, however in this case I can not store complex results like this:
            <S:Envelope xmlns:S=" http://schemas.xmlsoap.org/soap/envelope/">
            <S:Body>
            <ns2:GetUsersResponse xmlns:ns2="urn:satpath/service">
            <users>
            <active>No</active>
            <address>----</address>
            <createdBy>----</createdBy>
            <department>----</department>
            <email>----</email>
            <loginID>admin</loginID>
            <phone>----</phone>
            <privilege>Manager</privilege>
            <superVisor>----</superVisor>
            <userName>----</userName>
            </users>
            <users>
            <active>No</active>
            <address>----</address>
            <createdBy>----</createdBy>
            <department>----</department>
            <email>----</email>
            <loginID>meena</loginID>
            <phone>----</phone>
            <privilege>Network Operator</privilege>
            <superVisor>----</superVisor>
            <userName>----</userName>
            </users>
            </ns2:GetUsersResponse>
            </S:Body>
            </S:Envelope>

            Actually I have a data grid in which I am displaying results obtained by web service, result is a bean which has all the details related to a user in the system like userName, loginID, Address, phone etc. I am getting all the result and displaying them by <mx:Datagrid dataprovider="myService.myOperation.lastResult" > and its working fine.

            Now I want to display 3 columns (user name, log in id and user privilege) when I click on one row I want to use states and transictions so that I can display user deatils (all other information obtained from the web service) for that I have to store the result somewhere possibly in a bean so that I can fetch it in the changed state( which is user detail) based on user has selected which row.

            I don't know how can I store the result in the bean at flex side, if I declare resultFormat as object I am unable to typecast it in some collection, if I take xml as result format soap headers are creating problem. Or should I follow some other approach? Please help its really urgent.

            Thanks for all the help & support.
            • 3. Re: Java Webservice and Flex
              SujitG Level 2
              Hi,

              I am assuming that you are getting list of user objects. In this case, Flex application will give you the user objects in an ArrayCollection. You can access it using event.result as ArrayCollection. Once you have the ArrayCollection, you can set it as the dataProvider for the DataGrid. When an item is clicked on the DataGrid, DataGrid.selectedItem will return the Object selected.
              You can even try mapping your AS objects to Java objects. If you create a class called User and map it to the bean on the server, the bean objects returned from Java will be serialized to User objects by the Flex application.

              Please find more details on mapping AS objects to Java objects at this URL
              http://sujitreddyg.wordpress.com/2008/01/16/mapping-action-script-objects-to-java-objects/

              Please find more details on web service result object serialization in Flex at this URL
              http://livedocs.adobe.com/flex/3/html/data_access_3.html#202409

              Hope this helps.
              • 4. Re: Java Webservice and Flex
                meena_r Level 1
                Thanks you so much Sujit, it worked!