views:

27

answers:

1

Here is the code-

private void getImageIDs()
        {
            Uri serviceUri = new Uri("https://api.sqlazureservices.com/NasaService.svc/MER/Images?missionId=1&$format=raw");
            WebClient recDownloader = new WebClient();
            recDownloader.Headers["$accountKey"] = "<enter your key>";
            recDownloader.Headers["$uniqueUserID"] = "<enter your id>";
            recDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(recDownloader_OpenReadCompleted);
            recDownloader.OpenReadAsync(serviceUri);            
        }

        private void recDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Stream responseStream = e.Result;
                XNamespace ns = "http://www.w3.org/2005/Atom";
                XElement marsStuff = XElement.Load(responseStream);
                entries = marsStuff.Elements(ns + "entry");
                string imageID = (string)entries.ElementAt<XElement>(index).Element(ns + "title").Value;
                Console.WriteLine(imageID);
                getImage(imageID);                   
            }
        }

        private void getImage(string ID)
        {
            Uri serviceUri = new Uri("https://api.sqlazureservices.com/NasaService.svc/MER/Images/" + ID + "?$format=raw");
            WebClient imgDownloader = new WebClient();
            imgDownloader.Headers["$accountKey"] = "<enter your key>";
            imgDownloader.Headers["$uniqueUserID"] = "<enter your id>";
            imgDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(imgDownloader_OpenReadCompleted);
            imgDownloader.OpenReadAsync(serviceUri);
        }

        private void imgDownloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Stream imageStream = e.Result;
                BitmapImage imgsrc = new BitmapImage();
                imgsrc.SetSource(imageStream);
                MarsImage.Source = imgsrc;
            }
        }

        private void appbar_BackButton_Click(object sender, EventArgs e)
        {
            if (index > 0)
            {
                index--;
                XNamespace ns = "http://www.w3.org/2005/Atom";
                string imageID = (string)entries.ElementAt<XElement>(index).Element(ns + "Title").Value;
                getImage(imageID);
            }
        }

        private void appbar_ForwardButton_Click(object sender, EventArgs e)
        {
            if ( (index + 1) < entries.Count<XElement>())
            {
                index++;
                XNamespace ns = "http://www.w3.org/2005/Atom";
                string imageID = (string)entries.ElementAt<XElement>(index).Element(ns + "Title").Value;
                getImage(imageID);
            }
        }
    }

I am not seeing any images. Anybody able to get this sample running?

+2  A: 

This one? http://msdn.microsoft.com/en-us/magazine/gg232764.aspx

If so, there are some things that had to change to make it work, as the original code was on a preview version of the WP7 tools, as listed in the comments there:

Delete (or comment out) the following lines of code from MainPage.xaml.cs        
recDownloader.Headers["$accountKey"] = "<Your account key>";    
recDownloader.Headers["$uniqueUserID"] = "<Your user ID>";
imgDownloader.Headers["$accountKey"] = "<Your account key>";
imgDownloader.Headers["$uniqueUserID"] = "<Your user ID>";

Replace the two recDownloader lines of code with:

recDownloader.Credentials = new NetworkCredential("accountKey", "<Your account key>");   

Replace the two imgDownloader lines of code with:

imgDownloader.Credentials = new NetworkCredential("accountKey", "<Your account key>");   
John Gardner
Now I can see the first image but when I click next I get an error on the following line asking me to create a new instance - string imageID = (string)entries.ElementAt<XElement>(index).Element(ns + "Title").Value;
CodeToGlory
what specific error are you getting? it might deserve a new question...
John Gardner
System.NullReferenceException was unhandled Message=NullReferenceException StackTrace:at MarsImageViewer.MainPage.appbar_ForwardButton_Click(Object sender, EventArgs e) at Microsoft.Phone.Shell.ApplicationBarItemContainer.FireEventHandler(EventHandler handler, Object sender, EventArgs args) at Microsoft.Phone.Shell.ApplicationBarIconButton.ClickEvent() at Microsoft.Phone.Shell.ApplicationBarIconButtonContainer.ClickEvent() at Microsoft.Phone.Shell.ApplicationBar.OnCommand(UInt32 idCommand) at Microsoft.Phone.Shell.Interop.NativeCallbackInteropWrapper.OnCommand(UInt32 idCommand)
CodeToGlory
sounds like you should just fire up the debugger and see what's null. The method's only 4 lines long. then look at the xml returned by your service call and see what's wrong.
John Gardner
I did debug and it looks like the service is not returning valid xml and it is failing because of that and now it is out of my hands. Thanks for your help. I am marking your answer as the correct answer. Thanks for the help!
CodeToGlory
Having problems too. I have my account key which I believe is this:http://www.screencast.com/users/CarloDekstop/folders/Jing/media/cf7736ca-a6c6-42f7-bf50-30982d4ce72aAnd the uniqueUserId is my email right? "[email protected]". If so, the code should be:recDownloader.Credentials = new NetworkCredential("accountKey", "XXxxX06XxXx1xxXXxxxXXXXXXxX+4XXxx8x3gxxxXXXX=");recDownloader.Credentials = new NetworkCredential("uniqueUserID", "[email protected]");Is that right? If it is, for some reason it's still not running over here. Any help?
Carlo