6 Replies Latest reply on Apr 9, 2010 5:31 PM by avdave

    Problem loading Image through XML!

    avdave Level 1

      Hi,

        I am Akshay and am beginner in this forum. I am trying to load images using flickr API and having problem.

       

      I am doing it in following fashion:

       

       

       

      1) I am calling the Httpservice to get the xml through flickr API as

       

      <s:HTTPService id="photoService" url="http://api.flickr.com/services/feeds/....FullAPI" resultFormat="e4x" result="photoResultHandler(event);" fault="photoFaultHandler(event);"/>

       

      2) In photoResultHandler I am retrieving the XML

       

       

      private function photoResultHandler(re:ResultEvent):void

      {

      photoXml= re.result as XML;

       

      }

       

      However, I am struggeling to retireve elements from photoXML and then use it to display image in :

      <mx:Image  id="img0" x="151" y="98" width="520" height="291"/>

       

      The XML structure is as below:

       

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

      <feed">

      <entry>

           <author>  </author>

           <link rel="enclosure" type="image/jpeg" href="http://farm5.static.flickr.com/4484460580_04bbced2d8_o.jpg"/>

      </entry>

      <entry>

      ..

      ..

      </entry>

       

      I need to load the href value of link  which can be found in feed-->entry-->link in the above xml.

       

      Please help!

       

      -Akshay

        • 1. Re: Problem loading Image through XML!
          dave cragg Level 2

          As an example:

           

          private function photoResultHandler(re:ResultEvent):void

          {

              photoXml= re.result as XML;

              img0.source = photoXML.entry[0].link[0].@href

           

          }

           

          That will set the source of Image  img0) to the href of the first link element of the the first entry element.

          • 2. Re: Problem loading Image through XML!
            avdave Level 1

            Thanks Dave,

               I tried that but in vain....the source= photoXML.entry[0].link[0].@href but actually not converting it to actual path on runtime I donno why

             

            I tried debbuging it and actually getting it value of source as null

             

            any thouhgts?

             

             

            -Thanks

            • 3. Re: Problem loading Image through XML!
              dave cragg Level 2

              I'm not sure what the problem is.

               

              Are you sure about the example XML structure you posted? Can you post a real example?

               

              I tried the url you posted, and although it redirected to a "not available" image, the redirected image displayed in the Image control.

              • 4. Re: Problem loading Image through XML!
                avdave Level 1

                Hello Dave,

                   Thanks for your prompt reply. Here's the mxml code :

                 

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

                <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                   xmlns:s="library://ns.adobe.com/flex/spark"

                   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" creationComplete="photoService.send()"  minHeight="600">

                 

                <fx:Script>

                <![CDATA[

                import mx.controls.Alert;

                import mx.events.FlexEvent;

                import mx.events.MoveEvent;

                import mx.rpc.events.FaultEvent;

                import mx.rpc.events.ResultEvent;

                 

                [Bindable]

                public var photoXml:XML;

                 

                 

                private function photoResultHandler(re:ResultEvent):void

                {

                photoXml= re.result as XML;

                //var src :int =  "{photoXml.feed.entry.updated}" as int;

                img0.source= String(photoXml.entry[0].link[1].@href);

                 

                 

                }

                 

                private function photoFaultHandler(fe:FaultEvent):void

                {

                Alert.show(fe.fault.message);

                }

                 

                 

                 

                ]]>

                  </fx:Script>

                 

                <fx:Declarations>

                <!-- Place non-visual elements (e.g., services, value objects) here -->

                <s:HTTPService id="photoService" url="http://api.flickr.com/services/feeds/photos_public.gne?id=48951865@N07" resultFormat="e4x" result="photoResultHandler(event);" fault="photoFaultHandler(event);"/>

                 

                 

                 

                 

                 

                </fx:Declarations>

                <mx:Image  id="img0" x="151" y="98" width="520" height="291"/>

                </s:Application>

                 

                Hope it helps you in finding out the problem...DO let me know your thouhgts

                 

                Thanks again for your replies so far......

                 

                -Akshay

                • 5. Re: Problem loading Image through XML!
                  Peter deHaan Level 4

                  I'm sure somebody can clean this up quite a bit, but this may help get you started. I think you're getting caught up by the default XML namespace:

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                 xmlns:s="library://ns.adobe.com/flex/spark" 
                                 xmlns:mx="library://ns.adobe.com/flex/mx"
                                 creationComplete="photoService.send();">
                      
                      <fx:Script>
                          <![CDATA[
                              import mx.controls.Alert;
                              import mx.rpc.events.FaultEvent;
                              import mx.rpc.events.ResultEvent;
                              
                              [Bindable]
                              public var photoXMLList:XMLList;
                              
                              private function photoResultHandler(re:ResultEvent):void {
                                  var nsDefault:Namespace = new Namespace('http://www.w3.org/2005/Atom');
                                  default xml namespace = nsDefault;
                                  
                                  var tXML:XML = re.result as XML;
                                  photoXMLList = tXML.entry.link.(@rel == 'enclosure').@href;
                                  
                                  img0.source = photoXMLList[0];
                              }
                              
                              private function photoFaultHandler(fe:FaultEvent):void {
                                  Alert.show(fe.fault.message);
                              }
                          ]]>
                      </fx:Script> 
                      
                      <fx:Declarations>
                          <s:HTTPService id="photoService"
                                         url="http://api.flickr.com/services/feeds/photos_public.gne?id=48951865@N07"
                                         resultFormat="e4x"
                                         result="photoResultHandler(event);"
                                         fault="photoFaultHandler(event);"/>
                      </fx:Declarations>
                      
                      <mx:Image  id="img0" x="151" y="98" width="520" height="291"/>
                      
                  </s:Application>
                  
                   
                   

                   

                  Peter

                  • 6. Re: Problem loading Image through XML!
                    avdave Level 1

                    Thanks Peter,

                       That's great help. This is exactly what I was looking for.

                     

                       I am surprised though how the default namespace helped.

                     

                    One last point, it's taking a while to load the image and I need loading around 15 images at a time .   If you can share your thoughts on this it would be great!

                     

                    Thanks for your previous response again!

                     

                    -Akshay