0 Replies Latest reply on Aug 21, 2008 5:02 AM by uday_kumar

    How to get data from web service

    uday_kumar
      HI
      I'm new to Flex.

      I'm able to connect the webservie. But i'm unable to get dta from webserive.

      How to do this. Please help in this.I'm placing my code here

      TestServie.java

      import javax.ejb.Stateless;
      import javax.jws.WebMethod;
      import javax.jws.WebParam;
      import javax.jws.WebResult;
      import javax.jws.WebService;

      import org.jboss.wsf.spi.annotation.WebContext;

      @WebService
      @Stateless
      @WebContext(contextRoot = "/progrator-webservices")
      public class TestService {
      @WebMethod

      public String sayHello(String text)
      {
      System.out.println("In Web Service"+text);
      if(text==null)
      {
      return "You typed Nothing".toString();
      }else
      return ("You Typed "+text).toString();
      }
      }

      ===============GreetMe.mxml
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      xmlns:login="test.*"
      layout="absolute"
      applicationComplete="applicationComplete();">
      <mx:Script>
      <![CDATA[
      import mx.controls.Alert;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;
      import mx.utils.ObjectUtil;

      import flash.trace.Trace;
      import test.TestService;
      public static var wsdlBaseUrl:String;
      private function greetMe(x:String):void
      {
      // if(url.substr(0,4)=="file") {
      // wsdlBaseUrl=" http://127.0.0.1:8080/progrator-webservices/";
      // } else {
      // wsdlBaseUrl=url.substring(0,url.indexOf("/",7))+"/progrator-webservices/";
      // }

      outPut.text=TestService.sayHello(textBox.text);
      //outPut.text="Hello "+x;
      textBox.text="";
      textBox.focusEnabled;

      }
      public function applicationComplete():void{
      wsdlBaseUrl=" http://127.0.0.1:8080/progrator-webservices/";
      TestService.initService();

      }
      private function getString(evt:ResultEvent):void {
      outPut.text = ObjectUtil.toString(evt.result);

      }


      ]]>
      </mx:Script>
      <mx:TextInput name="type" id="textBox" x="131" y="103" width="119"/>
      <mx:Button name="GreetMe" label="GreetMe" id="greet" x="153.5" y="133"
      click="greetMe(textBox.text)"/>
      <mx:Button name="GreetMe1" label="GreetMe1" id="greet1" x="153.5" y="233"
      click="ws.sayHello.send()"/>
      <mx:Label id="outPut" name="outPut" x="94" y="163" width="221"/>
      <mx:WebService id="ws" wsdl=" http://localhost:8080/progrator-webservices/TestService?wsdl">
      <mx:operation name="sayHello">
      <mx:request>
      <!--<String>{textBox.text}</String>-->
      <text>{textBox.text}</text>
      </mx:request>
      </mx:operation>
      </mx:WebService>


      </mx:Application>
      =============TestService.as

      package test
      {
      import mx.controls.Alert;
      import mx.managers.CursorManager;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.soap.WebService;
      public class TestService
      {
      private static var ws:WebService;
      [Bindable]
      public static var outPut:String="";
      [Bindable]
      private static var text:String;
      public static function echoResultHandler(event:ResultEvent):void {
      //mx.controls.Alert(event.result.toString());
      outPut=event.result.toString();
      // var retStr:String = event.result.echoStr;
      // var retInt:int = event.result.echoInt;
      //Do something.
      }
      public static function initService():void
      {

      ws = new WebService();
      //location of the Web Service Description
      ws.wsdl = GreetMe.wsdlBaseUrl+"TestService?wsdl";
      mx.controls.Alert.show(ws.wsdl);
      ws.loadWSDL();
      //CursorManager.setBusyCursor();
      }
      public static function sayHello(text:String):String {

      TestService.text=text;
      //ws = new WebService();
      //ws.wsdl = GreetMe.wsdlBaseUrl+"TestService?wsdl";
      outPut=ws.sayHello(text);
      ws.sayHello.addEventListener(ResultEvent.RESULT,echoResultHandler);
      mx.controls.Alert.show("Hey"+outPut.toString());

      //outPut="";

      CursorManager.removeBusyCursor();
      return outPut;


      }
      public function myfunction(event:ResultEvent):void
      {
      mx.controls.Alert(event.result.echoStr);
      var retStr:String = event.result.echoStr;
      var retInt:int = event.result.echoInt;
      }

      }
      }

      I tryed in two ways one with using <mx:webservie> other is with button Click i'm calling action Script.

      But i'm unable to get data
      it is giving [objec Async Token] as result.

      Every thing is perfect except the result from web service