tags:

views:

164

answers:

2

In my project I used Google adwords API and also Yahoo API. I want to send request to two API at same time by using flex.

Is it possible to send request to same time. If not how can I do ?

+3  A: 

Since flex is event based, this should be pretty simple. Just create 2 URLRequest objects (I am assuming both APIs are REST-based), and set your GET variables. Create a loadComplete event listener for each of the URLRequest object, and in each of the eventlisteners, write code that processes the results from both APIs.

That should do it.

futureelite7
Thanks futureelite7 for response me . answer is very nice but if i set eventlistener any one event only at time . so how can i do for both api request object . Could u tell me any referense url
R.Vijayakumar
you add an eventlistener to each of the two URLRequest objects. So each URLRequest object will dispatch an event to the listener. You can use the same handler if you like.
futureelite7
A: 

Not "at the same time," no. For one thing, you've got only one (background) thread to work with, and that thread can only do one thing at a time, so however you write your code, one of the requests will always be initiated first.

If what you're asking is whether you can send out a request to both services in a single call, then the answer is again no (since both will have different URLs, and the ActionScript APIs don't take arrays as URL parameters, they only take strings).

That said, I suppose you could "fake it," so to speak, by having your Flex app call a service you wrote yourself -- e.g., in C# -- and have that service call (synchronously) Google, then Yahoo, then return control back to your Flex app along with a modified result set consisting of whatever happened to come back from Google and Yahoo, respectively.

Unless I'm misunderstanding (or overthinking) the question. ;)

Here's some sample code illustrating one way to handle calling two services -- not concurrently, but one right after the other, using mx.rpc.HTTPService:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onCreationComplete()">

    <mx:Script>
     <![CDATA[

      import mx.rpc.http.HTTPService;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;

      private function onCreationComplete():void
      {
       callGoogle();
       callYahoo();
      }

      private function callGoogle():void
      {
       var svc:HTTPService = new HTTPService();
       svc.url = "http://api.google.com/someservice.xml";
       svc.addEventListener(ResultEvent.RESULT, onGoogleResult, false, 0, true);
       svc.addEventListener(FaultEvent.FAULT, onGoogleFault, false, 0, true);
       svc.send();
      }

      private function callYahoo():void
      {
       var svc:HTTPService = new HTTPService();
       svc.url = "http://api.yahoo.com/someservice.xml";
       svc.addEventListener(ResultEvent.RESULT, onYahooResult, false, 0, true);
       svc.addEventListener(FaultEvent.FAULT, onYahooFault, false, 0, true);
       svc.send();
      }

      private function onGoogleResult(event:ResultEvent):void
      {
       trace(event.result);
      }

      private function onGoogleFault(event:FaultEvent):void
      {
       trace(event.fault.message);
      }

      private function onYahooResult(event:ResultEvent):void
      {
       trace(event.result);
      }

      private function onYahooFault(event:FaultEvent):void
      {
       trace(event.fault.message);
      }

     ]]>
    </mx:Script>

</mx:Application>

Hope that helps! Post back with comments and I'll keep an eye out.

Christian Nunciato