Finish Networking Phone 7, iPhone Networking




CS185c

Chris Pollett

Oct. 13, 2010

Outline

Introduction and Review

Phone 7 Filesystem, Sockets, and More

How to process Data Sources

Example RSS Document

<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:ynews="http://news.yahoo.com/rss/">
<channel>
<title>Yahoo! News: Top Stories</title>
<copyright>Copyright (c) 2010 Yahoo! Inc. All rights reserved.</copyright>
<link>http://news.yahoo.com/top-stories</link>
<category>topstories</category>
<description>Top Stories</description>
<language>en-us</language> 
<lastBuildDate>Wed, 13 Oct 2010 19:40:30 GMT</lastBuildDate>

<ttl>5</ttl> 
<image>
<title>Yahoo! News</title>
<width>142</width>
<height>18</height>
<link>http://news.yahoo.com/top-stories</link>
<url>http://l.yimg.com/a/i/us/nws/th/main_142c.gif</url>
</image>
<item>
<title>20 miners free; Chile rescue past halfway mark 
    (AP)
</title>

 <link>http://us.rd.yahoo.com/dailynews/rss/topstories/*http://news.yahoo.com/s/ap/20101013/ap_on_re_la_am_ca/lt_chile_mine_collapse</link>
 <guid isPermaLink="false">ap/20101013/lt_chile_mine_collapse</guid>
<source>AP</source>
<category>world</category>
<pubDate>Wed, 13 Oct 2010 19:06:00 GMT</pubDate>
<description>&#60;p>&#60;a href="http://us.rd.yahoo.com/dailynews/rss/topstories/*http://news.yahoo.com/s/ap/20101013/ap_on_re_la_am_ca/lt_chile_mine_collapse">&#60;img src="http://d.yimg.com/a/p/ap/20101013/capt.c9e04375d841434dacd9a7d0fe948cd9-c9e04375d841434dacd9a7d0fe948cd9-0.jpg?x=130&amp;y=86&amp;q=85&amp;sig=gYhF500qE..xP1I.3gV47Q--" align="left" height="86" width="130" alt="In this photo released by the Chilean government, miner Mario Gomez, left, gestures as he meets his wife for the first time after being rescued from the collapsed San Jose gold and copper mine where he had been trapped with 32 other miners for over two months near Copiapo, Chile, Wednesday, Oct. 13, 2010.  (AP Photo/Hugo Infante, Chilean government)" border="0" />&#60;/a>AP - The miners who spent 69 agonizing days deep under the Chilean earth were hoisted one by one to freedom Wednesday, their rescue moving with remarkable speed while their countrymen erupted in cheers and the world watched transfixed.&#60;/p>&#60;br clear="all"/></description>

<media:content url="http://d.yimg.com/a/p/ap/20101013/capt.c9e04375d841434dacd9a7d0fe948cd9-c9e04375d841434dacd9a7d0fe948cd9-0.jpg?x=130&amp;y=86&amp;q=85&amp;sig=gYhF500qE..xP1I.3gV47Q--" type="image/jpeg" height="86" width="130"/>
<media:text type="html">&#60;p>&#60;a href="http://us.rd.yahoo.com/dailynews/rss/topstories/*http://news.yahoo.com/s/ap/20101013/ap_on_re_la_am_ca/lt_chile_mine_collapse">&#60;img src="http://d.yimg.com/a/p/ap/20101013/capt.c9e04375d841434dacd9a7d0fe948cd9-c9e04375d841434dacd9a7d0fe948cd9-0.jpg?x=130&amp;y=86&amp;q=85&amp;sig=gYhF500qE..xP1I.3gV47Q--" align="left" height="86" width="130" alt="photo" title="In this photo released by the Chilean government, miner Mario Gomez, left, gestures as he meets his wife for the first time after being rescued from the collapsed San Jose gold and copper mine where he had been trapped with 32 other miners for over two months near Copiapo, Chile, Wednesday, Oct. 13, 2010.  (AP Photo/Hugo Infante, Chilean government)" border="0"/>&#60;/a>&#60;/p>&#60;br clear="all"/></media:text>
<media:credit role="publishing company">(AP)</media:credit>
</item>

....<!-- More item's -->
</channel>
</rss>

Demo Web App

MainPage.xaml

<phone:PhoneApplicationPage 
    x:Class="ParseWebContentTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY FEED" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Height="75" HorizontalAlignment="Left" Name="textBox1" Text="https://www.pollett.org/users/cpollett/blog/index.rss" VerticalAlignment="Top" Width="293" Margin="9,0,0,0" />
            <Button Content="Read" Height="72" HorizontalAlignment="Left" Margin="296,3,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
            <ListBox Height="610" HorizontalAlignment="Left" Margin="10,72,0,0" Name="listBox1" VerticalAlignment="Top" Width="460" >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Height="130">
                            <StackPanel Width="370">
                                <TextBlock Text="{Binding Title}" Foreground="#FFC8AB14" FontSize="28" />
                                <TextBlock Text="{Binding Link}" TextWrapping="Wrap" FontSize="24" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

More Demo Web App

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq; // to add this you need to first do an Add Reference for your project...
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace ParseWebContentTest
{
    /* This will be a little class to hold an RSS item
       Notice how we can quickly set up accessors methods in C #
    */
    public class RssItem
    {
        public string Title { get; set; }
        public string Link { get; set; }
    }

    //Here is the class where the work is done
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        // Handles Read button clisk and start downloading RSS page
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebClient myrss = new WebClient(); // WebClient is analog to HttpUrlConnection for Android
            string site = textBox1.Text;

            // set up a callback for when the feed has been downloaded
            myrss.DownloadStringCompleted += new DownloadStringCompletedEventHandler(myrss_DownloadStringCompleted);

            //start the download but don't block
            myrss.DownloadStringAsync(new Uri(site));
        }

        // handle the downloaded feed items
        void myrss_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null) return;

            XElement rssItems = XElement.Parse(e.Result);//here is where we parse the RSS feed

            /*
                Here is where we use Linq. Notice the syntax is similar 
                to SQL but order is from .. where .. select
                The thing analagous to a SQL table in this case are the item's in the RSS feed.
                The Descendants("item") is acting roughly like a syntax-checked XPath query.
                A "row" returned roughly will correspond to a title and link.
             */
            listBox1.ItemsSource = from item in rssItems.Descendants("item") 
                                   select new RssItem
                                   {
                                       Title = item.Element("title").Value,
                                       Link = item.Element("link").Value
                                   };

        }
    }
}

Demo RSS App screenshot

RSS App screenshot

Networking on the iPhone

Some UIWebView and UiWebViewDelegate methods

Getting the Response into Data