<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CodeCube.NET</title>
	<atom:link href="http://codecube.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://codecube.net</link>
	<description>Joel Martinez' weblog</description>
	<lastBuildDate>Thu, 13 Dec 2012 14:57:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>C# 5 First Look</title>
		<link>http://codecube.net/2012/12/c-5-first-look/</link>
		<comments>http://codecube.net/2012/12/c-5-first-look/#comments</comments>
		<pubDate>Thu, 13 Dec 2012 14:57:29 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=625</guid>
		<description><![CDATA[My book is finally available for pre-order! From the overview: Learn about all the latest features of C#, including the asynchronous programming capabilities that promise to make apps ultra-responsive Examine how C# evolved over the years to be more expressive, easier to write, and how those early design decisions enabled future innovations Explore the language’s [...]]]></description>
				<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://www.packtpub.com/csharp-5-first-look/book"><img class="size-full wp-image-626 aligncenter" title="C# 5 First Look" alt="C# 5 First Look" src="http://codecube.net/wp-content/uploads/2012/12/6761EN_mockupcover_normal.png" width="175" height="213" /></a></p>
<p>My book is finally <a href="http://www.packtpub.com/csharp-5-first-look/book">available for pre-order</a>! From the overview:</p>
<blockquote>
<ul>
<li>Learn about all the latest features of C#, including the asynchronous programming capabilities that promise to make apps ultra-responsive</li>
<li>Examine how C# evolved over the years to be more expressive, easier to write, and how those early design decisions enabled future innovations</li>
<li>Explore the language’s bright future building applications for other platforms using the Mono Framework</li>
</ul>
</blockquote>
<p>In the later chapters of the book, you learn to build 3 different applications, the first is a Flickr viewer for Windows RT</p>
<p><a href="http://codecube.net/wp-content/uploads/2012/12/6761_04_03.png"><img class="aligncenter size-medium wp-image-627" alt="Flickr Viewer" src="http://codecube.net/wp-content/uploads/2012/12/6761_04_03-300x168.png" width="300" height="168" /></a></p>
<p>&nbsp;</p>
<p>The second is an HTML5 Mobile app using ASP.NET MVC that uses the geolocation APIs and <a href="http://signalr.net/">SignalR</a> to build a &#8220;SoLoMo&#8221; experience.</p>
<p><a href="http://codecube.net/wp-content/uploads/2012/12/6761_05_05.png"><img class="aligncenter size-medium wp-image-628" alt="Join Up" src="http://codecube.net/wp-content/uploads/2012/12/6761_05_05-300x225.png" width="300" height="225" /></a></p>
<p>&nbsp;</p>
<p>And finally, using <a href="http://www.mono-project.com/MonoMac">MonoMac</a> to build a Mac App using C# 5 (though, irony of ironies, Xamarin <a href="http://xamarin.com/mac">announces Xamarin.Mac</a> <strong>yesterday</strong>).</p>
<p><a href="http://codecube.net/wp-content/uploads/2012/12/6761_06_06.png"><img class="aligncenter size-medium wp-image-629" alt="Mac App" src="http://codecube.net/wp-content/uploads/2012/12/6761_06_06-300x190.png" width="300" height="190" /></a></p>
<p>It was a pleasure working with everyone from Packt on this title. Special thanks to <a href="https://twitter.com/asherdevuyst">Asher DeVuyst</a> for reviewing the book and giving me great feedback <img src='http://codecube.net/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/12/c-5-first-look/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introducing Viewer for Khan Academy (Windows 8)</title>
		<link>http://codecube.net/2012/09/introducing-viewer-for-khan-academy-windows-8/</link>
		<comments>http://codecube.net/2012/09/introducing-viewer-for-khan-academy-windows-8/#comments</comments>
		<pubDate>Thu, 27 Sep 2012 00:32:02 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[Apps]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=605</guid>
		<description><![CDATA[After much hard work, I&#8217;m happy to announce the release of a project I&#8217;ve been working on, Viewer for Khan Academy on the Windows Store (windows 8). The source code can be found on GitHub. I really respect and appreciate what they&#8217;re doing over at the Khan Academy. With two small children of my own, [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://apps.microsoft.com/webpdp/en-US/app/viewer-for-khan-academy/f9c07088-9f3e-4312-ad5b-0f3b4aeb9b37"><img class="aligncenter" title="Viewer for Khan Academy" src="https://raw.github.com/joelmartinez/Khan-Academy-for-WinRT/master/SubmissionAssets/414x180.png" alt="Viewer for Khan Academy" width="414" height="180" /></a></p>
<p>After much hard work, I&#8217;m happy to announce the release of a project I&#8217;ve been working on, <a href="http://apps.microsoft.com/webpdp/en-US/app/viewer-for-khan-academy/f9c07088-9f3e-4312-ad5b-0f3b4aeb9b37">Viewer for Khan Academy on the Windows Store</a> (windows 8). The source code can be <a href="https://github.com/joelmartinez/Khan-Academy-for-WinRT">found on GitHub</a>.</p>
<p>I really respect and appreciate what they&#8217;re doing over at the <a href="http://khanacademy.org">Khan Academy</a>. With two small children of my own, anything that I can do to bring awareness to their great content and help enrich tomorrow&#8217;s generation (and even today&#8217;s) is time well spent in my book.</p>
<p>Huge shoutout to the folks over at Channel 9, who have produced a <a href="http://channel9.msdn.com/Series/Migrating-apps-from-Windows-Phone-to-Windows-8">great set of tutorials</a> and videos on the development of v1 of the app. The tutorials are written from the perspective of porting the <a href="https://github.com/joelmartinez/Khan-Academy-for-Windows-Phone">Windows Phone 7</a> version of the app to Windows 8. Many thanks and props to <a href="https://twitter.com/rickbarraza">Rick</a>, <a href="https://twitter.com/BrianPeek">Brian</a>, <a href="https://twitter.com/ClintRutkas">Clint</a>, and <a href="https://twitter.com/danielfe">Dan</a>. And of course, all of this wouldn&#8217;t be possible without the effort put forth by the <a href="http://www.khanacademy.org/about/the-team">Khan Academy Team</a>.</p>
<p>Thanks!</p>
<p><a href="http://codecube.net/wp-content/uploads/2012/09/screen1.png"><img class="alignnone size-thumbnail wp-image-610" title="" src="http://codecube.net/wp-content/uploads/2012/09/screen1-150x150.png" alt="" width="150" height="150" /></a> <a href="http://codecube.net/wp-content/uploads/2012/09/screen2.png"><img class="alignnone size-thumbnail wp-image-611" title="" src="http://codecube.net/wp-content/uploads/2012/09/screen2-150x150.png" alt="" width="150" height="150" /></a> <a href="http://codecube.net/wp-content/uploads/2012/09/screen3.png"><img class="alignnone size-thumbnail wp-image-612" title="" src="http://codecube.net/wp-content/uploads/2012/09/screen3-150x150.png" alt="" width="150" height="150" /></a> <a href="http://codecube.net/wp-content/uploads/2012/09/screen4.png"><img class="alignnone size-thumbnail wp-image-613" title="" src="http://codecube.net/wp-content/uploads/2012/09/screen4-150x150.png" alt="" width="150" height="150" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/09/introducing-viewer-for-khan-academy-windows-8/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Universal Subtitles C# API Wrapper</title>
		<link>http://codecube.net/2012/08/universal-subtitles-c-api-wrapper/</link>
		<comments>http://codecube.net/2012/08/universal-subtitles-c-api-wrapper/#comments</comments>
		<pubDate>Fri, 03 Aug 2012 03:44:22 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=597</guid>
		<description><![CDATA[This is available on GitHub here: https://github.com/joelmartinez/universalsubtitles-csharp C# 5.0 API Wrapper for the Universal Subtitles service, which is what the Khan Academy uses as their provider. I&#8217;m building this to add subtitles to the Khan Academy for Windows RT app. Usage is simple: using UniversalSubtitles; ... var api = new UniversalSubtitles.v1.Api(); string video_url = "http://www.youtube.com/watch?v=W0VWO4asgmk&#38;feature=youtube_gdata_player"; SubtitleLine[] subtitles = await [...]]]></description>
				<content:encoded><![CDATA[<p><em>This is available on GitHub here: <a href="https://github.com/joelmartinez/universalsubtitles-csharp">https://github.com/joelmartinez/universalsubtitles-csharp</a></em></p>
<p>C# 5.0 API Wrapper for the <a href="http://www.universalsubtitles.org">Universal Subtitles</a> service, which is what the <a href="http://khanacademy.org">Khan Academy</a> uses as their provider. I&#8217;m building this to add subtitles to the <a href="https://github.com/joelmartinez/Khan-Academy-for-WinRT">Khan Academy for Windows RT</a> app.</p>
<p>Usage is simple:</p>
<blockquote>
<pre>using UniversalSubtitles;
...
var api = new UniversalSubtitles.v1.Api();

string video_url = "http://www.youtube.com/watch?v=W0VWO4asgmk&amp;feature=youtube_gdata_player";

SubtitleLine[] subtitles = await api.SubtitlesAsync(video_url);
// returns the subtitles in the video's spoken language</pre>
</blockquote>
<p>You can also give the user a list of available languages that are available for this video</p>
<blockquote>
<pre>Language[] languages = await api.LanguagesAsync(video_url);
// display the list of available languages

SubtitleLine[] subtitles = await api.SubtitlesAsync(video_url, "it"); 
// or you can pass an instance of Language</pre>
</blockquote>
<p>Hope it&#8217;s useful for someone out there <img src='http://codecube.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/08/universal-subtitles-c-api-wrapper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Back in Orlando</title>
		<link>http://codecube.net/2012/07/back-in-orlando/</link>
		<comments>http://codecube.net/2012/07/back-in-orlando/#comments</comments>
		<pubDate>Fri, 27 Jul 2012 02:04:14 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=586</guid>
		<description><![CDATA[Well that was fun It was only two years ago that I moved to NYC. In that time, I made a ton of new friends, had great conversations, and expanded my horizons by changing my focus to mobile development. It was an amazing experience, and I&#8217;m glad I had it. That being said, I am [...]]]></description>
				<content:encoded><![CDATA[<p>Well that was fun <img src='http://codecube.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  It was only two years ago that I <a href="http://codecube.net/2010/06/moving-to-new-york/">moved to NYC</a>. In that time, I made a ton of new friends, had great conversations, and expanded my horizons by changing my focus to mobile development. It was an amazing experience, and I&#8217;m glad I had it. That being said, I am glad to be back in Orlando amongst family and friends. Working from home will be an interesting experience. I&#8217;ve done it before, but always under slightly different circumstances. I plan on visiting several <a href="http://urbanrethink.com/">co-working</a> <a href="http://orlando.colabusa.com/">spaces</a> from time to time &#8230; there&#8217;s been lots of <a href="http://www.floridatechnologyjournal.com/latest-video?slg=ftj-live-orlandos-tech-boom&amp;orderby=latest">exciting activity</a> going on in Orlando, so I&#8217;m looking forward to getting to know more of the tech community here.</p>
<p>But, as they say, time stands still for no man; so I&#8217;ve already got a few events coming up that you should totally come to and participate in!</p>
<ul>
<li>I will be a judge for an upcoming contest being run by the <a href="http://owpug.tattoocoder.com/">Orlando Windows Phone user group</a>. The contest is to build a windows phone or windows 8 app using the <a href="https://developer.darkskyapp.com/">Dark Sky API</a>, for which I wrote an <a href="https://github.com/joelmartinez/darksky-csharp">API wrapper</a>:<br />
<a href="http://blog.tattoocoder.com/2012/07/orlando-windows-phone-metro-user-group.html">http://blog.tattoocoder.com/2012/07/orlando-windows-phone-metro-user-group.html</a></li>
<li>August 23rd, I will be speaking at the <a href="http://onetug.org">Orlando .NET User Group</a> on writing <a href="http://twilio.com">Twilio</a> apps:<br />
<a href="http://www.onetug.org/Events/12-07-13/August_23rd_-_Joel_Martinez_presents_Twilio_is_the_Dealio.aspx">http://www.onetug.org/Events/12-07-13/August_23rd_-_Joel_Martinez_presents_Twilio_is_the_Dealio.aspx</a></li>
</ul>
<p>There&#8217;s a few other things in the works &#8230; it&#8217;s an amazing time to be a developer <img src='http://codecube.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/07/back-in-orlando/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twilio Request Parameters in ASP.NET MVC</title>
		<link>http://codecube.net/2012/07/twilio-request-parameters-in-asp-net-mvc/</link>
		<comments>http://codecube.net/2012/07/twilio-request-parameters-in-asp-net-mvc/#comments</comments>
		<pubDate>Tue, 10 Jul 2012 00:20:00 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=582</guid>
		<description><![CDATA[Integrating Twilio into your ASP.NET MVC applications is now just a tiny bit easier &#8230; The latest twilio-csharp project contains a few new classes which can be used as the parameter of your SMS, Voice, and Status endpoints: VoiceRequest SmsRequest StatusCallbackRequest This makes it so that the MVC framework will automatically map the incoming parameters [...]]]></description>
				<content:encoded><![CDATA[<p>Integrating Twilio into your ASP.NET MVC applications is now just a tiny bit easier &#8230; The latest <a href="https://github.com/twilio/twilio-csharp">twilio-csharp</a> project contains a few new classes which can be used as the parameter of your SMS, Voice, and Status endpoints:</p>
<ul>
<li><a href="https://github.com/twilio/twilio-csharp/blob/master/src/Twilio.Mvc/VoiceRequest.cs">VoiceRequest</a></li>
<li><a href="https://github.com/twilio/twilio-csharp/blob/master/src/Twilio.Mvc/SmsRequest.cs">SmsRequest</a></li>
<li><a href="https://github.com/twilio/twilio-csharp/blob/master/src/Twilio.Mvc/StatusCallbackRequest.cs">StatusCallbackRequest</a></li>
</ul>
<p>This makes it so that the MVC framework will automatically map the incoming parameters into a strongly typed object:</p>
<blockquote>
<pre>public ActionResult sms(SmsRequest request)
{
    var response = new TwilioResponse();
    response.Sms(string.Format("You said '{0}'", request.Body));

    return TwiML(response);
}</pre>
</blockquote>
<p>These changes were merged in with <a href="http://codecube.net/2012/05/twilio-csharp-for-monotouch-and-android/">the pull request</a>, which was originally meant to only include the MonoTouch and Mono For Android projects. But I had made these additional changes in preparation for the talk I&#8217;ll be giving at <a href="http://onetug.org">ONETUG on using Twilio</a>. So those changes snuck right in to the main lib <img src='http://codecube.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/07/twilio-request-parameters-in-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multi-Armed Bandit in C#</title>
		<link>http://codecube.net/2012/05/multi-armed-bandit-in-c/</link>
		<comments>http://codecube.net/2012/05/multi-armed-bandit-in-c/#comments</comments>
		<pubDate>Fri, 01 Jun 2012 03:31:46 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=571</guid>
		<description><![CDATA[After reading this fantastic blog post about how to do better than A/B testing (http://stevehanov.ca/blog/index.php?id=132), I decided to create an implementation of the algorithm in C#. As I already had a library dealing with statistical decision making on GitHub, I&#8217;ve added this functionality the nBayes library, which you can find here: https://github.com/joelmartinez/nBayes &#8211; specifically, in [...]]]></description>
				<content:encoded><![CDATA[<p>After reading this fantastic blog post about how to do better than A/B testing (<a href="http://stevehanov.ca/blog/index.php?id=132"><em>http://stevehanov.ca/blog/index.php?id=132</em></a>), I decided to create an implementation of the algorithm in C#. As I already had a library dealing with statistical decision making on GitHub, I&#8217;ve added this functionality the nBayes library, which you can find here:</p>
<p><a href="https://github.com/joelmartinez/nBayes">https://github.com/joelmartinez/nBayes</a> &#8211; specifically, in the <a href="https://github.com/joelmartinez/nBayes/tree/master/nBayes/Optimization">Optimization namespace</a>.</p>
<p>I won&#8217;t reiterate the pros/cons, and explanation of this algorithm, as Steve does a fantastic job over on his blog &#8230; so I&#8217;ll just present the sample program I wrote to go along with it. This program uses the API to create three options (the same used in the blog post), and then proceeds to simulate users who prefer to act on one option, then partway (35%) through the exercise changing preference. And you can see how the algorithm correctly begins to primarily show the new choice about halfway through.</p>
<blockquote>
<pre>/// &lt;summary&gt;This sample program has 3 options, and will use the the optimizer
/// to run a test scenario. Two options are picked, and we will simulate user interest in one,
/// then user sentiment changing to favor the second option.&lt;/summary&gt;
private static void TestOptimizer()
{
  var optimizer = new Optimizer();

  // define the available options
  Option[] options = new Option[] {
    Option.Named("Orange"),
    Option.Named("Green"),
    Option.Named("White")
  };
  optimizer.Add(options[0]);
  optimizer.Add(options[1]);
  optimizer.Add(options[2]);

  // pick two options, and define when user interest will change
  var firstWinner = options[2]; // white
  var secondWinner = options[1]; // green
  var switchRatio = .35f; // 35% of the way through the test set
  int tries = 100; // the test set

  for (int i = 0; i &lt; tries; i++) {
    Option selected = optimizer.Choose().Result; // don't care about asyncrony in this particular example
    Console.WriteLine("Choosing: {0}", selected);

    // decide which chosen option 'the users' will prefer
    bool isFirstBatch = (float)tries * switchRatio &gt; i;
    if (isFirstBatch &amp;&amp; selected == firstWinner)
      firstWinner.IncrementSuccesses();
    else if (!isFirstBatch &amp;&amp; selected == secondWinner)
      secondWinner.IncrementSuccesses();
  }

  Console.WriteLine("\nResults! We expect that ({0}) will have the highest success rate, and ({1}) will be in second place", secondWinner, firstWinner);
  Console.WriteLine("\nThis is the final result after {0} tries\n{1}", tries, optimizer);
}</pre>
</blockquote>
<p>There are multiple strategies to solve the &#8216;<a href="http://en.wikipedia.org/wiki/Multi-armed_bandit">multi-armed bandit</a>&#8216; problem. This particular implementation is using &#8216;Epsilon-Greedy&#8217;. It&#8217;s still early, and I have other ideas on how to structure the code in order to facilitate multiple strategies &#8230; but for now, let me know if you&#8217;ve got any thoughts or feedback on this bit of code. Thanks! <img src='http://codecube.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/05/multi-armed-bandit-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DarkSky API Wrapper for C#</title>
		<link>http://codecube.net/2012/05/darksky-api-wrapper-for-c/</link>
		<comments>http://codecube.net/2012/05/darksky-api-wrapper-for-c/#comments</comments>
		<pubDate>Wed, 30 May 2012 03:58:52 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=563</guid>
		<description><![CDATA[When I saw that the guys from Dark Sky were releasing an API, I knew I wanted to work with it. So I whipped up a quick C# API Wrapper for the DarkSky API. You can find the project here on GitHub: https://github.com/joelmartinez/darksky-csharp Currently supported platforms are: Windows Phone, iOS, and Android via MonoTouch/Mono for [...]]]></description>
				<content:encoded><![CDATA[<p>When I saw that the guys from <a href="http://darkskyapp.com">Dark Sky</a> were <a href="http://journal.darkskyapp.com/2012/dark-sky-for-developers/">releasing an API</a>, I knew I wanted to work with it. So I whipped up a quick C# API Wrapper for the <a href="https://developer.darkskyapp.com/">DarkSky API</a>.</p>
<p>You can find the project here on GitHub: <a href="https://github.com/joelmartinez/darksky-csharp">https://github.com/joelmartinez/darksky-csharp</a></p>
<p>Currently supported platforms are: Windows Phone, iOS, and Android via MonoTouch/Mono for Android.</p>
<p>Usage is very simple:</p>
<blockquote>
<pre>using DarkSky;

...

var darksky = new DarkSky.Api(YOUR_API_KEY);
var orlando  = new Position { Latitude=28.5381f, Longitude=-81.3794f };
Task&lt;FullForecast&gt; response = darksky.Forecast(orlando);

response.ContinueWith(forecast =&gt; DisplayText(forecast.Result.HourSummary));
// displays something like "Rain starting in 3 Min, Stopping 30 Min Later"</pre>
</blockquote>
<p>Please let me know if you have any feedback either by opening <a href="https://github.com/joelmartinez/darksky-csharp/issues">an issue</a>, <a href="mailto:joelmartinez@gmail.com">emailing me</a>, <a href="http://codecube.net">commenting on my blog</a>, or forking the project and submitting a pull request.</p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/05/darksky-api-wrapper-for-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Problem with C# 5&#8242;s async/await Pattern</title>
		<link>http://codecube.net/2012/05/the-problem-with-c-5s-asyncawait-pattern/</link>
		<comments>http://codecube.net/2012/05/the-problem-with-c-5s-asyncawait-pattern/#comments</comments>
		<pubDate>Mon, 21 May 2012 13:44:45 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=556</guid>
		<description><![CDATA[C# 5 brings a fantastic new feature … built-in asynchrony (not to be confused with concurrency). The compiler has added two new keywords, async and await, which allows your code to transparently change execution contexts. For example, instead of writing: Task .StartNew(() =&#62; MakeSomeDecisionSlowly()) .ContinueWith(result =&#62; ProcessResult(result)); You can simply say: bool result = await [...]]]></description>
				<content:encoded><![CDATA[<p>C# 5 brings a fantastic new feature … <a href="http://msdn.microsoft.com/en-us/library/hh191443%28v=vs.110%29.aspx">built-in asynchrony</a> (not to be <a href="http://stackoverflow.com/a/4056583/5416">confused with concurrency</a>). The compiler has added two new keywords, async and await, which allows your code to transparently change execution contexts. For example, instead of writing:</p>
<blockquote>
<pre>Task
  .StartNew(() =&gt; MakeSomeDecisionSlowly())
  .ContinueWith(result =&gt; ProcessResult(result));</pre>
</blockquote>
<p>You can simply say:</p>
<blockquote>
<pre>bool result = await MakeSomeDecisionSlowly();
ProcessResult(result);</pre>
</blockquote>
<p>While the two pieces of code are exactly equivalent, the second code is so much simpler to understand. Because you don&#8217;t really have to think about the fact that there is an asynchronous context switch going on, you are free to focus on your program&#8217;s logic, rather than worrying about manually orchestrating what tasks are doing what.</p>
<p>However, after using these features for several days while porting my <a href="https://github.com/joelmartinez/Khan-Academy-for-Windows-Phone">KhanAcademy windows phone app to windows 8</a>, I&#8217;ve come to approach them with a bit of caution. While they are great for low level IO-bound tasks, in my opinion they don&#8217;t scale well to higher level API design. A few things to watch out for:</p>
<p>If you use await somewhere in your code, then the containing method needs to be marked with the async keyword, and there are specific rules about return types. Namely, the method must either be void, or return Task&lt;T&gt;. For example, the example above would have to be in a method</p>
<blockquote>
<pre>public async void DoThatThing() { … }</pre>
</blockquote>
<p>Or, if you wanted to return the result of &#8216;MakeSomeDecisionSlowly&#8217; back to the caller, it would have to look like this:</p>
<blockquote>
<pre>public async Task&lt;bool&gt; DoThatThing() { … }</pre>
</blockquote>
<p>Which is great, and all, but then it means that the calling method must be decorated with &#8216;async&#8217; as well. If you are refactoring an existing codebase, this can have the effect of rippling up the inheritance chain with numerous (albeit small) changes … depending on the situation.</p>
<p>Ok, fine, maybe that&#8217;s not a huge deal; it&#8217;s kind of annoying but I can definitely deal with that kind of refactoring. The big problem I have with async/await is that outside of relatively trivial pieces of code, it can tend to wipe away many benefits of proper abstractions.</p>
<p>Let&#8217;s say you are implementing a remote API call, and you have several requirements:</p>
<ul>
<li>Process the results if
<ul>
<li>You have a network connection (common mobile requirement)</li>
<li>The server is reachable (what if you&#8217;re connected at starbucks, but haven&#8217;t accepted the wifi&#8217;s terms of use?)</li>
<li>The server returns a code 200 (bugs happen)</li>
<li>The server indicates that your credentials are ok (what if the user&#8217;s password expired, or was changed by the user on your website?)</li>
<li>The server&#8217;s json response indicates a successful request</li>
</ul>
</li>
<li>If any of the above conditions are false, notify the user of the problem (with specific text for the situation).</li>
</ul>
<p>This is a very common scenario in any occasionally connected mobile application, and you can see that there are no less than five distinct decisions that need to be made during the course of an API call. If you use async/await blindly, you have two problems:</p>
<p>First, you are deferring these decisions to the caller … you&#8217;ll do a nice asynchronous call, and return a Task&lt;string&gt; with the json results; or if you&#8217;re sophisticated, a Task&lt;T&gt; with the results already parsed and converted to a strongly typed object. However, it&#8217;s up to the caller to handle exceptions, check for network connections, deal with individual response scenarios (logged out, invalid request, etc.). So you may end up with every call site looking like this:</p>
<blockquote>
<pre>if (HasConnection())
{
  try
  {
    MyObject result = await MakeRemoteAPICall();
    ProcessSuccessfulResult(result);
  }
  catch(Exception e)
  {
    ProcessError(e.Message);
  }
}
else
{
  ProcessError("No Connection");
}</pre>
</blockquote>
<p>Second, providing further abstractions is difficult because you are limited to a single return type. So if you want to handle all of the stated requirements for the caller, you will have to wrap up the result in a special return value that can provide some details to the user, such as &#8216;was it successful&#8217;, the error message if it wasn&#8217;t, and the parsed return value if it was. While the pros and cons of using error codes for return values have been <a href="https://www.google.com/search?q=error+codes+vs+exception&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a">debated for ages</a>; I tend to prefer simpler return values for local APIs, since a complex return value adds an additional burden on the caller to know how to interpret the results. At best, every call site would look something like this:</p>
<blockquote>
<pre>Result&lt;MyObject&gt; value = await MakeRemoteAPICall();
if (value.WasSuccessful)
{
  ProcessSuccessfulResult(value.Result);
}
else
{
  ProcessError(value.ErrorMessage);
}</pre>
</blockquote>
<p>That&#8217;s a bit better than before, but still rather verbose for my taste.</p>
<p>I propose that the callback style of API design still has a place for situations like this. Consider the following sample:</p>
<blockquote>
<pre>MakeRemoteAPICall(
  result =&gt; ProcessSucessfulResult(result),
  error =&gt; ProcessError(error));</pre>
</blockquote>
<p>For the local consumer of this API, this is logically the same: if the call was successful do this, otherwise do that. But the caller doesn&#8217;t have to worry about all the nuances of deciding whether a call was successful or not. They can focus on how to process the results, rather than dealing with all of this ceremony. And after all, isn&#8217;t that what we&#8217;re all aiming to do, simplify our code?</p>
<p>I would love to hear thoughts about how this kind of code can be made even simpler. Would love to hear any opinions, for or against. Thanks!</p>
<p><em>edit: lively debate over on Reddit <img src='http://codecube.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  <a href="http://www.reddit.com/r/programming/comments/txhfq/the_problem_with_c_5s_asyncawait_pattern/">http://www.reddit.com/r/programming/comments/txhfq/the_problem_with_c_5s_asyncawait_pattern/</a></em></p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/05/the-problem-with-c-5s-asyncawait-pattern/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Twilio-CSharp for MonoTouch and Android</title>
		<link>http://codecube.net/2012/05/twilio-csharp-for-monotouch-and-android/</link>
		<comments>http://codecube.net/2012/05/twilio-csharp-for-monotouch-and-android/#comments</comments>
		<pubDate>Fri, 18 May 2012 02:28:13 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=550</guid>
		<description><![CDATA[I noticed that Twilio&#8216;s official C# client didn&#8217;t have a version that you could use from MonoTouch and Mono for Android. So I took a few minutes, forked the project, and added some MonoDevelop projects so that you can compile for each of those two platforms. The mono versions are using the silverlight version of [...]]]></description>
				<content:encoded><![CDATA[<p>I noticed that <a href="http://twilio.com">Twilio</a>&#8216;s official <a href="https://github.com/twilio/twilio-csharp">C# client</a> didn&#8217;t have a version that you could use from <a href="http://xamarin.com/monotouch">MonoTouch</a> and <a href="http://xamarin.com/monoforandroid">Mono for Android</a>. So I took a few minutes, <a href="https://github.com/joelmartinez/twilio-csharp">forked the project</a>, and added some <a href="http://monodevelop.com/">MonoDevelop</a> projects so that you can compile for each of those two platforms.</p>
<p>The mono versions are using the silverlight version of the API, which uses async style methods where you pass in callbacks. There are two dependencies: <a href="https://github.com/restsharp/RestSharp">RestSharp</a>, and<a href="http://json.codeplex.com/"> json.net</a>. Though it looks like RestSharp has shed their dependency on json.net, there are still some references in the twilio code. The json.net project maintain mono specific versions &#8230; they depend on a portable library for that cross platform support. Unfortunately that means that you can&#8217;t compile it if you&#8217;re on a mac and only using monodevelop. Thankfully I found <a href="https://github.com/ayoung/Newtonsoft.Json">this helpful fork</a> that did this work for me.</p>
<p>At the risk of  <a href="http://john-sheehan.com/post/17964678895/dont-push-your-pull-requests">&#8216;pushing&#8217; my pull request</a> onto them, I submitted <a href="https://github.com/twilio/twilio-csharp/pull/28">a pull request</a>; But I made sure to word it such that I&#8217;m ok if they want to push back and ask for any changes &#8230; or maybe they don&#8217;t want to maintain a Mono version at all, in which case I&#8217;m happy to keep it in my fork. So we&#8217;ll see what happens. Please let me know if you have any feedback on this change.</p>
<p>As an aside, I also noticed while I was looking around github that many projects that have mono versions tend to use *.MonoTouch and *.MonoDroid for their project/solution names. It&#8217;s a subtle thing, but  Xamarin is careful not to refer to their product as MonoDroid, but Mono For Android. I wonder if this has to do with the trademarks that are <a href="http://www.businessinsider.com/verizon-drops-droid-brand-for-newest-android-phone-2010-2">owned by Lucasarts</a>, if so I&#8217;d hate to wake up to a nice C&amp;D in the mailbox <img src='http://codecube.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/05/twilio-csharp-for-monotouch-and-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parse an iOS plist on Android</title>
		<link>http://codecube.net/2012/05/parse-an-ios-plist-on-android/</link>
		<comments>http://codecube.net/2012/05/parse-an-ios-plist-on-android/#comments</comments>
		<pubDate>Wed, 16 May 2012 04:16:40 +0000</pubDate>
		<dc:creator>Joel Martinez</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codecube.net/?p=547</guid>
		<description><![CDATA[I was implementing a feature in the CalorieCount Android App the other day that was previously implemented in the iOS version of that same app. Just needed to show a list of timezones based on what country was selected. Not rocket science at all, but the iOS implementation was storing the data in a plist [...]]]></description>
				<content:encoded><![CDATA[<p>I was implementing a feature in the <a href="http://caloriecount.about.com/">CalorieCount</a> <a href="https://market.android.com/details?id=com.about.CalorieCount">Android App</a> the other day that was previously implemented in the <a href="http://itunes.apple.com/us/app/calorie-counter-by-caloriecount/id367018196?mt=8">iOS version</a> of that same app. Just needed to show a list of timezones based on what country was selected. Not rocket science at all, but the iOS implementation was storing the data in a plist file, with a &lt;dict&gt; element inside. So rather than take that data and try to translate it to something more &#8216;android specific&#8217; &#8230; I decided to just use the file as is. The biggest benefit was that in the future if we ever had to update the file, I could update it in place, and simply copy it over to the other platform.</p>
<p>So I copied the file into my xml folder, and wrote this simple parser that reads the file in, and parses it into a HashMap</p>
<div id="gist2655811" class="gist">
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-xmlmapparser-java-L1" rel="file-xmlmapparser-java-L1">1</span>
          <span class="line-number" id="file-xmlmapparser-java-L2" rel="file-xmlmapparser-java-L2">2</span>
          <span class="line-number" id="file-xmlmapparser-java-L3" rel="file-xmlmapparser-java-L3">3</span>
          <span class="line-number" id="file-xmlmapparser-java-L4" rel="file-xmlmapparser-java-L4">4</span>
          <span class="line-number" id="file-xmlmapparser-java-L5" rel="file-xmlmapparser-java-L5">5</span>
          <span class="line-number" id="file-xmlmapparser-java-L6" rel="file-xmlmapparser-java-L6">6</span>
          <span class="line-number" id="file-xmlmapparser-java-L7" rel="file-xmlmapparser-java-L7">7</span>
          <span class="line-number" id="file-xmlmapparser-java-L8" rel="file-xmlmapparser-java-L8">8</span>
          <span class="line-number" id="file-xmlmapparser-java-L9" rel="file-xmlmapparser-java-L9">9</span>
          <span class="line-number" id="file-xmlmapparser-java-L10" rel="file-xmlmapparser-java-L10">10</span>
          <span class="line-number" id="file-xmlmapparser-java-L11" rel="file-xmlmapparser-java-L11">11</span>
          <span class="line-number" id="file-xmlmapparser-java-L12" rel="file-xmlmapparser-java-L12">12</span>
          <span class="line-number" id="file-xmlmapparser-java-L13" rel="file-xmlmapparser-java-L13">13</span>
          <span class="line-number" id="file-xmlmapparser-java-L14" rel="file-xmlmapparser-java-L14">14</span>
          <span class="line-number" id="file-xmlmapparser-java-L15" rel="file-xmlmapparser-java-L15">15</span>
          <span class="line-number" id="file-xmlmapparser-java-L16" rel="file-xmlmapparser-java-L16">16</span>
          <span class="line-number" id="file-xmlmapparser-java-L17" rel="file-xmlmapparser-java-L17">17</span>
          <span class="line-number" id="file-xmlmapparser-java-L18" rel="file-xmlmapparser-java-L18">18</span>
          <span class="line-number" id="file-xmlmapparser-java-L19" rel="file-xmlmapparser-java-L19">19</span>
          <span class="line-number" id="file-xmlmapparser-java-L20" rel="file-xmlmapparser-java-L20">20</span>
          <span class="line-number" id="file-xmlmapparser-java-L21" rel="file-xmlmapparser-java-L21">21</span>
          <span class="line-number" id="file-xmlmapparser-java-L22" rel="file-xmlmapparser-java-L22">22</span>
          <span class="line-number" id="file-xmlmapparser-java-L23" rel="file-xmlmapparser-java-L23">23</span>
          <span class="line-number" id="file-xmlmapparser-java-L24" rel="file-xmlmapparser-java-L24">24</span>
          <span class="line-number" id="file-xmlmapparser-java-L25" rel="file-xmlmapparser-java-L25">25</span>
          <span class="line-number" id="file-xmlmapparser-java-L26" rel="file-xmlmapparser-java-L26">26</span>
          <span class="line-number" id="file-xmlmapparser-java-L27" rel="file-xmlmapparser-java-L27">27</span>
          <span class="line-number" id="file-xmlmapparser-java-L28" rel="file-xmlmapparser-java-L28">28</span>
          <span class="line-number" id="file-xmlmapparser-java-L29" rel="file-xmlmapparser-java-L29">29</span>
          <span class="line-number" id="file-xmlmapparser-java-L30" rel="file-xmlmapparser-java-L30">30</span>
          <span class="line-number" id="file-xmlmapparser-java-L31" rel="file-xmlmapparser-java-L31">31</span>
          <span class="line-number" id="file-xmlmapparser-java-L32" rel="file-xmlmapparser-java-L32">32</span>
          <span class="line-number" id="file-xmlmapparser-java-L33" rel="file-xmlmapparser-java-L33">33</span>
          <span class="line-number" id="file-xmlmapparser-java-L34" rel="file-xmlmapparser-java-L34">34</span>
          <span class="line-number" id="file-xmlmapparser-java-L35" rel="file-xmlmapparser-java-L35">35</span>
          <span class="line-number" id="file-xmlmapparser-java-L36" rel="file-xmlmapparser-java-L36">36</span>
          <span class="line-number" id="file-xmlmapparser-java-L37" rel="file-xmlmapparser-java-L37">37</span>
          <span class="line-number" id="file-xmlmapparser-java-L38" rel="file-xmlmapparser-java-L38">38</span>
          <span class="line-number" id="file-xmlmapparser-java-L39" rel="file-xmlmapparser-java-L39">39</span>
          <span class="line-number" id="file-xmlmapparser-java-L40" rel="file-xmlmapparser-java-L40">40</span>
          <span class="line-number" id="file-xmlmapparser-java-L41" rel="file-xmlmapparser-java-L41">41</span>
          <span class="line-number" id="file-xmlmapparser-java-L42" rel="file-xmlmapparser-java-L42">42</span>
          <span class="line-number" id="file-xmlmapparser-java-L43" rel="file-xmlmapparser-java-L43">43</span>
          <span class="line-number" id="file-xmlmapparser-java-L44" rel="file-xmlmapparser-java-L44">44</span>
          <span class="line-number" id="file-xmlmapparser-java-L45" rel="file-xmlmapparser-java-L45">45</span>
          <span class="line-number" id="file-xmlmapparser-java-L46" rel="file-xmlmapparser-java-L46">46</span>
          <span class="line-number" id="file-xmlmapparser-java-L47" rel="file-xmlmapparser-java-L47">47</span>
          <span class="line-number" id="file-xmlmapparser-java-L48" rel="file-xmlmapparser-java-L48">48</span>
          <span class="line-number" id="file-xmlmapparser-java-L49" rel="file-xmlmapparser-java-L49">49</span>
          <span class="line-number" id="file-xmlmapparser-java-L50" rel="file-xmlmapparser-java-L50">50</span>
          <span class="line-number" id="file-xmlmapparser-java-L51" rel="file-xmlmapparser-java-L51">51</span>
          <span class="line-number" id="file-xmlmapparser-java-L52" rel="file-xmlmapparser-java-L52">52</span>
          <span class="line-number" id="file-xmlmapparser-java-L53" rel="file-xmlmapparser-java-L53">53</span>
          <span class="line-number" id="file-xmlmapparser-java-L54" rel="file-xmlmapparser-java-L54">54</span>
          <span class="line-number" id="file-xmlmapparser-java-L55" rel="file-xmlmapparser-java-L55">55</span>
          <span class="line-number" id="file-xmlmapparser-java-L56" rel="file-xmlmapparser-java-L56">56</span>
          <span class="line-number" id="file-xmlmapparser-java-L57" rel="file-xmlmapparser-java-L57">57</span>
          <span class="line-number" id="file-xmlmapparser-java-L58" rel="file-xmlmapparser-java-L58">58</span>
          <span class="line-number" id="file-xmlmapparser-java-L59" rel="file-xmlmapparser-java-L59">59</span>
          <span class="line-number" id="file-xmlmapparser-java-L60" rel="file-xmlmapparser-java-L60">60</span>
          <span class="line-number" id="file-xmlmapparser-java-L61" rel="file-xmlmapparser-java-L61">61</span>
          <span class="line-number" id="file-xmlmapparser-java-L62" rel="file-xmlmapparser-java-L62">62</span>
          <span class="line-number" id="file-xmlmapparser-java-L63" rel="file-xmlmapparser-java-L63">63</span>
          <span class="line-number" id="file-xmlmapparser-java-L64" rel="file-xmlmapparser-java-L64">64</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-xmlmapparser-java-LC1"><span class="kn">package</span> <span class="n">cyborg</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC2">&nbsp;</div><div class="line" id="file-xmlmapparser-java-LC3"><span class="kn">import</span> <span class="nn">java.io.IOException</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC4"><span class="kn">import</span> <span class="nn">java.util.ArrayList</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC5"><span class="kn">import</span> <span class="nn">java.util.HashMap</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC6">&nbsp;</div><div class="line" id="file-xmlmapparser-java-LC7"><span class="kn">import</span> <span class="nn">org.xmlpull.v1.XmlPullParser</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC8"><span class="kn">import</span> <span class="nn">org.xmlpull.v1.XmlPullParserException</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC9">&nbsp;</div><div class="line" id="file-xmlmapparser-java-LC10"><span class="kn">import</span> <span class="nn">android.content.Context</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC11">&nbsp;</div><div class="line" id="file-xmlmapparser-java-LC12"><span class="cm">/**</span></div><div class="line" id="file-xmlmapparser-java-LC13"><span class="cm"> * This class parses an iOS plist with a dict element into a hashmap.</span></div><div class="line" id="file-xmlmapparser-java-LC14"><span class="cm"> */</span></div><div class="line" id="file-xmlmapparser-java-LC15"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">XmlMapParser</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC16">	<span class="kd">private</span> <span class="n">XmlPullParser</span> <span class="n">parser</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC17">&nbsp;</div><div class="line" id="file-xmlmapparser-java-LC18">	<span class="kd">public</span> <span class="nf">XmlMapVisitor</span><span class="o">(</span><span class="n">Context</span> <span class="n">context</span><span class="o">,</span> <span class="kt">int</span> <span class="n">xmlid</span><span class="o">)</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC19">		<span class="n">parser</span> <span class="o">=</span> <span class="n">context</span><span class="o">.</span><span class="na">getResources</span><span class="o">().</span><span class="na">getXml</span><span class="o">(</span><span class="n">xmlid</span><span class="o">);</span></div><div class="line" id="file-xmlmapparser-java-LC20">	<span class="o">}</span></div><div class="line" id="file-xmlmapparser-java-LC21">&nbsp;</div><div class="line" id="file-xmlmapparser-java-LC22">	<span class="kd">public</span> <span class="n">HashMap</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;&gt;</span> <span class="n">convert</span><span class="o">()</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC23">		<span class="n">HashMap</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;&gt;</span> <span class="n">map</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;</span><span class="n">String</span><span class="o">,</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;&gt;();</span></div><div class="line" id="file-xmlmapparser-java-LC24">&nbsp;</div><div class="line" id="file-xmlmapparser-java-LC25">		<span class="kd">final</span> <span class="n">String</span> <span class="n">KEY</span> <span class="o">=</span> <span class="s">&quot;key&quot;</span><span class="o">,</span> <span class="n">STRING</span> <span class="o">=</span> <span class="s">&quot;string&quot;</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC26">		</div><div class="line" id="file-xmlmapparser-java-LC27">		<span class="k">try</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC28">			<span class="n">parser</span><span class="o">.</span><span class="na">next</span><span class="o">();</span></div><div class="line" id="file-xmlmapparser-java-LC29">			<span class="kt">int</span> <span class="n">eventType</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="na">getEventType</span><span class="o">();</span></div><div class="line" id="file-xmlmapparser-java-LC30">			<span class="n">String</span> <span class="n">lastTag</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC31">			<span class="n">String</span> <span class="n">lastKey</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC32">			</div><div class="line" id="file-xmlmapparser-java-LC33">			<span class="k">while</span> <span class="o">(</span><span class="n">eventType</span> <span class="o">!=</span> <span class="n">XmlPullParser</span><span class="o">.</span><span class="na">END_DOCUMENT</span><span class="o">)</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC34">				</div><div class="line" id="file-xmlmapparser-java-LC35">				<span class="k">if</span> <span class="o">(</span><span class="n">eventType</span> <span class="o">==</span> <span class="n">XmlPullParser</span><span class="o">.</span><span class="na">START_TAG</span><span class="o">)</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC36">					<span class="n">lastTag</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="na">getName</span><span class="o">();</span></div><div class="line" id="file-xmlmapparser-java-LC37">				<span class="o">}</span> </div><div class="line" id="file-xmlmapparser-java-LC38">				<span class="k">else</span> <span class="nf">if</span> <span class="o">(</span><span class="n">eventType</span> <span class="o">==</span> <span class="n">XmlPullParser</span><span class="o">.</span><span class="na">TEXT</span><span class="o">)</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC39">					<span class="c1">// some text</span></div><div class="line" id="file-xmlmapparser-java-LC40">					<span class="k">if</span> <span class="o">(</span><span class="n">KEY</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="n">lastTag</span><span class="o">))</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC41">						<span class="c1">// start tracking a new key</span></div><div class="line" id="file-xmlmapparser-java-LC42">						<span class="n">lastKey</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="na">getText</span><span class="o">();</span></div><div class="line" id="file-xmlmapparser-java-LC43">					<span class="o">}</span></div><div class="line" id="file-xmlmapparser-java-LC44">					<span class="k">else</span> <span class="nf">if</span> <span class="o">(</span><span class="n">STRING</span><span class="o">.</span><span class="na">equalsIgnoreCase</span><span class="o">(</span><span class="n">lastTag</span><span class="o">))</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC45">						<span class="c1">// a new string for the last encountered key</span></div><div class="line" id="file-xmlmapparser-java-LC46">						<span class="k">if</span> <span class="o">(!</span><span class="n">map</span><span class="o">.</span><span class="na">containsKey</span><span class="o">(</span><span class="n">lastKey</span><span class="o">))</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC47">							<span class="n">map</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">lastKey</span><span class="o">,</span> <span class="k">new</span> <span class="n">ArrayList</span><span class="o">&lt;</span><span class="n">String</span><span class="o">&gt;());</span></div><div class="line" id="file-xmlmapparser-java-LC48">						<span class="o">}</span></div><div class="line" id="file-xmlmapparser-java-LC49">						</div><div class="line" id="file-xmlmapparser-java-LC50">						<span class="n">map</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">lastKey</span><span class="o">).</span><span class="na">add</span><span class="o">(</span><span class="n">parser</span><span class="o">.</span><span class="na">getText</span><span class="o">());</span></div><div class="line" id="file-xmlmapparser-java-LC51">					<span class="o">}</span></div><div class="line" id="file-xmlmapparser-java-LC52">				<span class="o">}</span></div><div class="line" id="file-xmlmapparser-java-LC53">				</div><div class="line" id="file-xmlmapparser-java-LC54">				<span class="n">eventType</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="na">next</span><span class="o">();</span></div><div class="line" id="file-xmlmapparser-java-LC55">			<span class="o">}</span></div><div class="line" id="file-xmlmapparser-java-LC56">		<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">XmlPullParserException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC57">			<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class="line" id="file-xmlmapparser-java-LC58">		<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class="line" id="file-xmlmapparser-java-LC59">			<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class="line" id="file-xmlmapparser-java-LC60">		<span class="o">}</span></div><div class="line" id="file-xmlmapparser-java-LC61">&nbsp;</div><div class="line" id="file-xmlmapparser-java-LC62">		<span class="k">return</span> <span class="n">map</span><span class="o">;</span></div><div class="line" id="file-xmlmapparser-java-LC63">	<span class="o">}</span></div><div class="line" id="file-xmlmapparser-java-LC64"><span class="o">}</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/joelmartinez/2655811/raw/a3966addf131f679edd00d91c245f4dde83f4f1b/XmlMapParser.java" style="float:right">view raw</a>
          <a href="https://gist.github.com/joelmartinez/2655811#file-xmlmapparser-java" style="float:right; margin-right:10px; color:#666;">XmlMapParser.java</a>
          <a href="https://gist.github.com/joelmartinez/2655811">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
      <div class="gist-file">
        <div class="gist-data gist-syntax">



  <div class="file-data">
    <table cellpadding="0" cellspacing="0" class="lines highlight">
      <tr>
        <td class="line-numbers">
          <span class="line-number" id="file-sample-plist-L1" rel="file-sample-plist-L1">1</span>
          <span class="line-number" id="file-sample-plist-L2" rel="file-sample-plist-L2">2</span>
          <span class="line-number" id="file-sample-plist-L3" rel="file-sample-plist-L3">3</span>
          <span class="line-number" id="file-sample-plist-L4" rel="file-sample-plist-L4">4</span>
          <span class="line-number" id="file-sample-plist-L5" rel="file-sample-plist-L5">5</span>
          <span class="line-number" id="file-sample-plist-L6" rel="file-sample-plist-L6">6</span>
          <span class="line-number" id="file-sample-plist-L7" rel="file-sample-plist-L7">7</span>
          <span class="line-number" id="file-sample-plist-L8" rel="file-sample-plist-L8">8</span>
          <span class="line-number" id="file-sample-plist-L9" rel="file-sample-plist-L9">9</span>
          <span class="line-number" id="file-sample-plist-L10" rel="file-sample-plist-L10">10</span>
          <span class="line-number" id="file-sample-plist-L11" rel="file-sample-plist-L11">11</span>
          <span class="line-number" id="file-sample-plist-L12" rel="file-sample-plist-L12">12</span>
          <span class="line-number" id="file-sample-plist-L13" rel="file-sample-plist-L13">13</span>
          <span class="line-number" id="file-sample-plist-L14" rel="file-sample-plist-L14">14</span>
          <span class="line-number" id="file-sample-plist-L15" rel="file-sample-plist-L15">15</span>
          <span class="line-number" id="file-sample-plist-L16" rel="file-sample-plist-L16">16</span>
          <span class="line-number" id="file-sample-plist-L17" rel="file-sample-plist-L17">17</span>
        </td>
        <td class="line-data">
          <pre class="line-pre"><div class="line" id="file-sample-plist-LC1"><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></div><div class="line" id="file-sample-plist-LC2"><span class="cp">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple Computer//DTD PLIST 1.0//EN&quot; &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span></div><div class="line" id="file-sample-plist-LC3"><span class="nt">&lt;plist</span> <span class="na">version=</span><span class="s">&quot;1.0&quot;</span><span class="nt">&gt;</span></div><div class="line" id="file-sample-plist-LC4"><span class="nt">&lt;dict&gt;</span></div><div class="line" id="file-sample-plist-LC5">	<span class="nt">&lt;key&gt;</span>key1<span class="nt">&lt;/key&gt;</span></div><div class="line" id="file-sample-plist-LC6">	<span class="nt">&lt;array&gt;</span></div><div class="line" id="file-sample-plist-LC7">		<span class="nt">&lt;string&gt;</span>one<span class="nt">&lt;/string&gt;</span></div><div class="line" id="file-sample-plist-LC8">		<span class="nt">&lt;string&gt;</span>two<span class="nt">&lt;/string&gt;</span></div><div class="line" id="file-sample-plist-LC9">	<span class="nt">&lt;/array&gt;</span></div><div class="line" id="file-sample-plist-LC10">	</div><div class="line" id="file-sample-plist-LC11">	<span class="nt">&lt;key&gt;</span>key2<span class="nt">&lt;/key&gt;</span></div><div class="line" id="file-sample-plist-LC12">	<span class="nt">&lt;array&gt;</span></div><div class="line" id="file-sample-plist-LC13">		<span class="nt">&lt;string&gt;</span>one<span class="nt">&lt;/string&gt;</span></div><div class="line" id="file-sample-plist-LC14">		<span class="nt">&lt;string&gt;</span>two<span class="nt">&lt;/string&gt;</span></div><div class="line" id="file-sample-plist-LC15">	<span class="nt">&lt;/array&gt;</span></div><div class="line" id="file-sample-plist-LC16"><span class="nt">&lt;/dict&gt;</span></div><div class="line" id="file-sample-plist-LC17"><span class="nt">&lt;/plist&gt;</span></div></pre>
        </td>
      </tr>
    </table>
  </div>

        </div>

        <div class="gist-meta">
          <a href="https://gist.github.com/joelmartinez/2655811/raw/62f2efa5d7885f431f345b04a8d57675eec960fb/sample.plist" style="float:right">view raw</a>
          <a href="https://gist.github.com/joelmartinez/2655811#file-sample-plist" style="float:right; margin-right:10px; color:#666;">sample.plist</a>
          <a href="https://gist.github.com/joelmartinez/2655811">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
        </div>
      </div>
</div>

<p>The way I look at it, may as well reuse what we&#8217;ve got, and spend less time yak shaving (ie. translating between platforms) &#8230; git &#8216;er done!</p>
]]></content:encoded>
			<wfw:commentRss>http://codecube.net/2012/05/parse-an-ios-plist-on-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
