Networking Android, Phone7




CS185c

Chris Pollett

Oct. 11, 2010

Outline

Overview of Networking

Clients and Servers

Checking Network Status

Creating a Server Socket

Quiz

Which of the following is true?

  1. NSOpenTimer is used to schedule a callback at a particular time on iPhone.
  2. Core Data is used to handle object relational mappings on iPhone.
  3. To make a TCP connection it suffices to know the IP address of where you are connecting to.

Communicating with a Server

HTTP

WebView

More on WebView

A Lower Level Way to Request a Page

Request a Page Using java.net

Using a WebBrowser in Phone 7

MainPage.xaml

<phone:PhoneApplicationPage 
    x:Class="WindowsPhoneApplication4.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="Browser Demo" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Content="Go" Height="72" HorizontalAlignment="Left" Margin="308,-16,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
            <phone:WebBrowser HorizontalAlignment="Left" Margin="12,116,0,0" Name="webBrowser1" VerticalAlignment="Top" Height="580" Width="444" />
            <Button Content="Go Saved" Height="72" HorizontalAlignment="Left" Margin="155,50,0,0" Name="button2" VerticalAlignment="Top" Width="160" Click="button2_Click" />
        </Grid>
        <TextBox Grid.RowSpan="2" Height="73" HorizontalAlignment="Left" Margin="12,56,0,0" Name="textBox1" Text="http://www.pollett.org" VerticalAlignment="Top" Width="330"  />
    </Grid>


</phone:PhoneApplicationPage>

MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
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;
using System.IO.IsolatedStorage;
using System.IO;

namespace WindowsPhoneApplication4
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        /// 
        /// When the 'Go' button is clicked, navigate to the web site.
        /// Save url in storeurl.txt
        /// 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string site;
            site = textBox1.Text;
            webBrowser1.Navigate(new Uri(site, UriKind.Absolute));
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())

            using (var file = appStorage.OpenFile("storeurl.txt", FileMode.OpenOrCreate))

            using (var writer = new StreamWriter(file))
            {
                writer.Write(site);
            }            
        }
        /// 
        /// When the 'Go Saved' button is clicked, read from
        /// storeurl.txt and navigate to the web site.
        /// 
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            //using means we call Dispose() automatically when done with the object
            // if use var rather than a type compiler will use type inference
            using (var appStorage = IsolatedStorageFile.GetUserStoreForApplication())

            using (var file = appStorage.OpenFile("storeurl.txt", FileMode.Open))
            {

                using (var reader = new StreamReader(file))
                {
                    textBox1.Text = reader.ReadLine();
                    webBrowser1.Navigate(new Uri(textBox1.Text, UriKind.Absolute));
                }

            }
        }
    }
}