Web Client and Windows Phone 7

While I was developing applications in windows phone 7, I came across many difficulties. Some are still unsolved some are solved. And the unsolved will be solved in near future :)

As Windows Phone is an upcoming platform in mobile industry, I selected it for developing many apps. And the development tools MS gives are really awesome and i think are far better than any other phone development :) (No offense).

One major problem I faced while I was making an app was how could I call a web page or how could I make any HTTP GET request in my application? This was a confusion for me. I knew how to do it in standard .Net framework but .Net framework available in Win Phone 7 is a subset of the original framework. So many approaches failed but I was unable to get what is the least possible effort I can make to do so.

After a long search and experiments, I finally found a neat way of doing it! This might be the simplest approach to call an HTTP resource in a Win Phone 7 app. Lets see how Web Client is useful in our case and write some code and then  I will try to explain how it works.

Lets call a RSS feed from Picasa web for our example and we will be able to see the whole XML from that feed in a TextBlock.

private void GetFeeds()

{

 string url = "http://picasaweb.google.com/data/feed/base/user/jos.brakband/albumid/5512094980648184513?alt=rss&kind=photo&hl=en_US";

 // A class in .Net framework to do the web stuff, without any mess :)

 WebClient webClient = new WebClient();

 // Event that will be called after successfully getting the page

 webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(FeedsDownloaded);

 // Two ways to get a url, one is simply call the url and wait for the result

 webClient.DownloadStringAsync(new Uri(url));

 // other is you can pass an object with it which will be available once url downloading is completed

 // webClient.DownloadStringAsync(new Uri(url), "Test String");

}

private void FeedsDownloaded(object sender, DownloadStringCompletedEventArgs e)

{

 string resultXml = e.Result;

 this.FeedXml.Text = resultXml;

 // You may get the passed test string by

 // String passedString = e.UserState as String;

}
See how simple is it to call any Web Resource using WebClient! There are more ways to do many other things like instead of a GET call what if we want to have a POST call or what if we want to download an image from this or what if I don’t want a string result but I need a IO Stream?
I will be writing many others posts on these topics. Stay tuned!
You can download above code here. Do comment if you have any question or suggestion.
Advertisements

5 Responses to “Web Client and Windows Phone 7”


  1. 2 Hassan Seth September 10, 2010 at 2:44 pm

    Welcome Amir Bhai.

    Thank You for joining us here.. and giving us a chance to explore the world =]

    Nice POST (Y), Really, Win Phone 7 is very simple. Waiting to read more on ‘Phones’ =]

    Best of Luck (Y)

  2. 3 Muzammil Peer December 21, 2010 at 7:38 pm

    For Json Parsing.

    public class LoginClass
    {
    public string LoginResult { get; set; }

    }

    String jsonstring = “{“LoginResult”:false}”; // json string.

    LoginClass result = JsonConvert.DeserializeObject(jsonstring);

    Inculde JsonNewton .dll file in ur windows phone 7 project.

    Deserializing is too easy using this library ….:P
    i have used this library in windows phone 7 and , i found similar library in Android (gson -json library).

  3. 5 afastian June 13, 2011 at 2:00 am

    nice :P Well my experience about web client was v good .. Except i was pretty not sure that when we call webclient again , in sense that if im doing all the webclient stuff on button press ,,, then when we press the button again n again ,, things go wrong SOMETIMES,..!! although on button press WebClient wb = new Webclient is also included…!
    I eaxctly wasnt able to solve this problem ,, Because it happened rarely ,, but i think webClient.CancelAsync helped ,,!

    Any sort of thing regarding the Abnormal Behavior of WEBCLIENT


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Tweets From The Author


%d bloggers like this: