3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client...
-
Upload
myles-cameron -
Category
Documents
-
view
228 -
download
0
description
Transcript of 3 4 5 6 7 8 9 WebClient client; 10 // Constructor public MainPage() { InitializeComponent(); client...
7Session 5.1
Connecting to a Data Service
Windows Phone
Topics Windows Phone applications on a
network The role of the Windows Phone
emulator Creating network connections Using asynchronous network
requests
Windows Phone3
Windows Phone on a network The Windows Phone device is very well connected WIFI 3G GPRS
All these networks are accessed by programs that use the TCP/IP networking protocol
Your program does not have to worry about the underlying communication technology
Windows Phone4
Phone limitations A mobile phone does not have a
working connection at all times Mobile applications that use the
network must either “fail gracefully” or provide offline modes for their users
Any network request must provide progress information and use timeouts to ensure that a failed network doesn’t cause them to get stuck
Windows Phone5
Windows Phone networking Windows Phone Silverlight
applications and XNA games can use the network But they are not allowed to make
“socket” based connections in Version 1.0 of the operating system
All network requests must be calls to services that will deliver a result asynchronously
The network calls are exactly the same as those provided in the desktop .NET libraries
Windows Phone6
Asynchronous connections A program will never make a call to
the network and then wait for the result This is called synchronous operation,
and is not supported by any network calls on the phone
Instead a program will make a call to the network to initiate an operation
The network object will then call back later with the result of the operation
Windows Phone7
Asynchronous operation This form of event based operation is
exactly the same as the model used by Silverlight to accept user input
Silverlight elements can generate program events Button pressed, Text Changed etc
A program can bind to these events so that a method is called when an event occurs
Windows Phone8
Downloading Web Pages To get started we could
look at an application to “scrape” text off a web page
This can be used to obtain data for display
We can also use it to read structured data from the web, of which more later
Windows Phone9
The WebClient class
To start we need something that will do the work for us
The .NET WebClient class can be used to fetch information from the internet An instance of the WebClient class
represents a single web request We can use it to read HTML from a
given web page
WebClient client;
Windows Phone10
Creating a WebClient
We can create a WebClient instance in the constructor for our page
// Constructorpublic MainPage(){ InitializeComponent();
client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler( client_DownloadStringCompleted);}
Windows Phone11
Binding to the loaded event
This statement binds a method to the completed event
The method runs after the page is fetched
// Constructorpublic MainPage(){ InitializeComponent();
client = new WebClient(); client.DownloadStringCompleted += new DownloadStringCompletedEventHandler( client_DownloadStringCompleted);}
Windows Phone12
Displaying the result
The client_DownloadStringCompleted method runs when the page text has arrived
It checks for an error and displays the text if there was none
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e){ if (e.Error == null) { pageTextBlock.Text = e.Result; }}
Windows Phone13
Sending the request
This is the event handler for the button that is pressed to start the transaction
It calls the DownloadStringAsync method on the WebClient to start the download
It is given a Uri to download from
private void loadButton_Click(object sender, RoutedEventArgs e){ client.DownloadStringAsync(new Uri(urlTextBox.Text));}
Windows Phone14
The Uri
A Uri instance gives the address of a resource
It can be created from a string of text In this program the Uri is created
from a TextBox on the user interface
private void loadButton_Click(object sender, RoutedEventArgs e){ client.DownloadStringAsync(new Uri(urlTextBox.Text));}
Windows Phone15
Demo 1: Web Scraping
Demo
Windows Phone16
Review Windows phones can make
connections to services over the network
All network requests are asynchronous The result of a network request is
delivered by an event generated in the client
The WebClient class provides a very easy way of loading the content of urls