I touched upon yahoo query language (YQL) interface in my last post. Today, I will show some quick practical uses of YQL and demonstrate how easy it is for the programmers to tap into this information.
For today’s example, we will examine the table named ‘weather.forecast’, which is available in the YQL interface.
Head over to the YQL Console to examine the parameters to be used for ‘weather.forecast’.
As shown in the screenshot, there is one parameter named ‘location’ that is required for querying data from this table.
1
SELECT *FROM weather.forecast WHERE location="63126"
The user enters the zip code for which weather forecast has to be displayed and press on ‘GO’ button.
The program takes this location, construct the correct URL and make a web request to retrieve the data from the internet.
Windows Phone SDK provides two options to make a web request and get data from the internet, WebClient and HTTPWebRequest. See here for more information.
For this example, I used the WebClient API for it’s inherent simplicity as this is a post more to demonstrate the YQL than about performance.
Since the program did not specify a output format, JQL returns XML by default. The output format can be specified as JSON by adding parameter to the original URL.
WebClient returns data on the same thread, so it can directly access the UI elements. On the other hand, HTTPWebRequest returns data on a different thread and the result has to be marshaled back to the UI thread. Since data is returned on a different thread, HTTPWebRequest has the advantage of not locking up the UI thread when it is processing.
1
2
3
4
// Update the UI elements
// Since the WebClient returns on the same thread, it can access the UI elements directly
Windows Phone 7 SDK has provided two classes for consuming resources from the web. WebClient and HTTPWebRequest.
HTTPWebRequest
HTTPWebRequest is a low level API for web requests. Unlike WebClient, it allows to tinker some of the HTTP aspects like cookies and user agent.
One major advantage of HTTPWebRequest is that it returns on another thread, which comes in handy for large web requests. It will not lock up the UI thread allowing the UI to remain responsive during the request. Since the response is returned on another thread the program should marshal the response back to the UI thread.
HTTPWebRequest is created by the static method HTTPWebRequest.Create().
// Get the response stream from the response object
using(Stream stream=response.GetResponseStream())
{
StreamReader reader=newStreamReader(stream);
data=reader.ReadToEnd();
}
}
WebClient
WebClient is a higher level abstraction of the web request. It wraps the HTTPWebRequest to provide a more easier API to make web requests.
WebClient has a built in mechanism to report progress of the request in percentage. WebClient runs on the same thread, so programs don’t have to use the dispatcher to pass data back to the UI thread.
However, since the WebClient runs on the UI thread, it can make the UI unresponsive if the program makes a long running web request.
1
2
3
4
5
6
7
8
9
10
11
{
WebClient client=newWebClient();
// Supports reporting the progress of the request.