C# style WebClient for Android

In last several months, i have been working on multiple mobile devices, like iPhone, WinPhone, Android and Palm Pre. I have came to a conclusion after making apps for these devices, that internet is quite a must in each application almost. And all platforms do support the web in quite an awesome way. Although i have tried every basic thing to run web in the device, but i found C#/.NET does it in a natural way that is more intuitive. It simply creates a WebClient and has call back style APIs, whenever any event is fired if we have attached any delegate for that event, that delegate is called. Thats quite AWESOME ! But while working on android i never found such thing in Java, that are delegates to call (Java 6).

And the HttpClient in android is although not a pain to call and work with, but its quite messy to just call a single url. I worked with it for many things, but was not satisfied. So after a while i was like tired of the HttpClient in android, so i tried to port the C# WebClient in android. For that i have to find a generic way that won’t make it even more pain to work with, i tried many things like Handlers and Threads (As it has to be thread safe), but they weren’t that good, the pain is still there. Needed to find a structure that is not verbose but works like a charm.

I came across AsyncTask in android, and it was an easy way to get rid of threads and was thread safe too. I liked it, and decided to make my WebClient a subclass of AsyncTask and it was quite nice to just create and object of this class and call a web page because it is a safe and easy way to handle threads, UI thread is not disturbed. Quite awesome, i found a way out to get out of the mess. But a thing that is left are delegates, else there is no use for it, because it won’t be a generic way to call the webpages.

I searched over net for any delegates thing i found, i found several ways. Two of them were Interface and Reflections (I know its costly). I thought if i would go for Interface any activity that will implement the interface can just have a single call back function to respond on any event WebClient would generate, that is cool but again there would be a mess in that single function that will implement it, there would be a whole lots of switch cases or if then else. Ah that would be bad again. Then the last option was Reflections API, i know, i know its costly. But the effectiveness is more costly than computing. So i decided to move on with Reflections API.

Method was the class that can do the stuff like delegates, but i wanted a better way to call them. So i decided to go for strings as the function name to pass instead of Method. So it makes the API look cool and easy to use, just pass the string as the name of the function, the Class in which this function exists, and if its an instance method pass the receivers instance else just null if the function is static. Well thats just how i made it, i have posted the whole source code on GITHUB, if you want it you may go there and have it, if you don’t want to get messy with code just download the jars and get started. More over i have added a README file over there that contains all the info you require to use the WebClient with examples. Hope you find it good and please comment in either cases if you like it or not.

I will be waiting impatiently for the comments. Thanks.

Link for GIT-HUB : WebClient for Android


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.