Question

I am currently developing a Mobile Application with Adobe Flex 4.6 and I am having an Issue with using the HTTP Service functionality. I am trying to use a HTTP request like shown below:

http://api.somesite.co.uk/api/v1/data?api_key=00000aaaaa111111&area=London

I have imported the service using the data menu and testing it using the parameters need and when i run a test on the service it pulls back the data fine and shows it in the "Test Operation" view.

However when I then Add the following code to my view to test the application it returns a 2032 error:

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:myservice="services.myservice.*"
        initialize="view1_initializeHandler(event)" title="list">
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;

            import spark.events.IndexChangeEvent;

            protected function view1_initializeHandler(event:FlexEvent):void
            {
                // TODO Auto-generated method stub
                var api_key:String = "00000aaaaa111111"
                var area:String = "London";

                listResult.addEventListener(ResultEvent.RESULT, onResult);
                listResult.addEventListener(FaultEvent.FAULT, httpFault);
                listResult.token = service.list(api_key, area);

                trace(listResult.token);
            }

            public function httpFault(event:FaultEvent):void { 
                var faultstring:String = event.fault.faultDetail; 
                trace("The Fault is: " + faultstring); 
            } 

            protected function onResult (event:ResultEvent):void
            {

            }

            protected function list_changeHandler(event:IndexChangeEvent):void
            {

            }

            protected function list_creationCompleteHandler(event:FlexEvent):void
            {

            }

        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:CallResponder id="listResult" result="data=listResult.lastResult"/>
        <myservice:MyService id="service"/>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <s:List id="list" left="0" right="0" top="0" bottom="85" fontSize="20" contentBackgroundColor="#272727"
            creationComplete="list_creationCompleteHandler(event)" change="list_changeHandler(event)">
        <s:AsyncListView list="{data}"/>
        <s:itemRenderer>
            <fx:Component>
                <s:IconItemRenderer
                    labelFunction="Name"
                    iconFunction="getImageURL"
                    iconHeight="80"
                    iconWidth="80">
                    <fx:Script>
                        <![CDATA[
                            import valueObjects.Item;

                            private function Name(item: Object):String
                            {
                                var item:Item = Item(item);
                                return listing.name;
                            }

                            private function getImageURL(item: Object):String
                            {
                                var item:Item = Item(item);
                                return listing.image_url;
                            }

                        ]]>
                    </fx:Script>
                </s:IconItemRenderer>
            </fx:Component>
        </s:itemRenderer>
    </s:List>

</s:View>

With the code above I have managed to track the issue to the fact that for some unknown stupid reason when the code is run from the view the url that is sent is wrong and the underscores are replaced by %5F as shown below:

http://api.somesite.co.uk/api/v1/data?area=London&api%5Fkey=00000aaaaa111111

Can anyone help me get around this issue?

Thanks

EDIT......

Hi,

If the URL was wrong I would expect it to fail when running it in the "Test Operation" view however it returns the data expected and all looks fine. Its only when I try to apply it to the view where I get the issue with it replacing the underscore for the api_key passed. The service I am using is a Zoopla one at the url below:

http://developer.zoopla.com/docs/Property_listings

If you could maybe look at this and let me know, I am passing 3 params:

  1. api_key
  2. listing_status
  3. area

From the debug data I can see that the underscores get changes and I have taken this changed url and tried to run that in another program and it fails, however when I simply change the api%5Fkey part to api_key and run it again in the other program it returns the data. So I believe the api_key underscore is the issue and I don't know how to solve this or why it doesnt work on the view but does in the Test view?? Any ideas?

EDIT 2...

I recently updated the code to this:

protected function list_creationCompleteHandler(event:FlexEvent):void
{   
    var params:Object = new Object();
    params.api_key = "MY_API_KEY";
    params.area = "London";
    params.listing_status = "rent";
    params.page_size = 100;
    srv.send(params);
}

<fx:Declarations>
    <s:HTTPService id="srv" url="http://api.zoopla.co.uk/api/v1/property_listings?"
                   method="GET" 
                   result="srv_resultHandler(event)" 
                   fault="srv_faultHandler(event)" useProxy="false"/>
</fx:Declarations>

When I use the above code I get this error below, it looks like the code is changing the URL in some way and creating a new messed up one :-S

The Fault is:

Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: http://localhost:37813/api/v1/property_listings?&page%5Fsize=100&api%5Fkey=MY_API_KEY&area=London&listing%5Fstatus=rent?hostport=api.zoopla.co.uk&https=N&id=F91A1C6D-8E19-9F50-EAEF-05DA5601D98D" errorID=2032]. URL: http://api.zoopla.co.uk/api/v1/property_listings?
Was it helpful?

Solution

ok, so first the server on the other end should be handling that be apparently isn't so...

The encoding of the params is a "feature". To prevent it, do not pass in a params object to the send method. Instead, manually build the request string like this:

var :myurl:String = "http://api.zoopla.co.uk/whatever?api_key=123&listing_status=45";
service.url = myurl;
service.send();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top