<?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>Neo Tech</title>
	<atom:link href="http://www.neo-tech.es/wp/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.neo-tech.es/wp</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 24 Mar 2012 02:24:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>A high-entropy randomness generator</title>
		<link>http://www.neo-tech.es/wp/?p=113</link>
		<comments>http://www.neo-tech.es/wp/?p=113#comments</comments>
		<pubDate>Sat, 24 Mar 2012 02:23:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Algoritmos]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[entropy]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[randomness]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=113</guid>
		<description><![CDATA[&#160; This cartoon of Dilbert always has always fascinated me. You can never be sure about randomness, since the concept itself of randomness provides uncertainty to the process. A few years ago, I even wrote a post on how to achieve randomness using deterministic methods. Nowadays, entropy can always be improved to obtain a more [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.neo-tech.es/wp/wp-content/uploads/2012/03/Bildschirmfoto-2012-03-24-um-03.02.34.png"><img class="size-full wp-image-114 aligncenter" title="Bildschirmfoto 2012-03-24 um 03.02.34" src="http://www.neo-tech.es/wp/wp-content/uploads/2012/03/Bildschirmfoto-2012-03-24-um-03.02.34.png" alt="" width="189" height="184" /></a></p>
<p>&nbsp;</p>
<p>This <a href="http://www.random.org/analysis/dilbert.jpg" target="_blank">cartoon</a> of Dilbert always has always fascinated me. You can never be sure about randomness, since the concept itself of randomness provides uncertainty to the process. A few years ago, I even wrote <a href="http://www.neo-tech.es/wp/?p=34" target="_blank">a post</a> on how to achieve randomness using deterministic methods. Nowadays, entropy can always be improved to obtain a more accurate (in this case would be more appropriate to say "less accurate" instead) result.  This can lead into many philosophical discussions, which are not my purpose.</p>
<p>The traditional approach has been to take contextual information (such as the UNIX time) to create a seed for the algorithm and give more uncertainty to the process. This might be sufficient for 99% of our purposes, but leads to more complex problems: For instance, is easy to determine when a user logged into a system, and if at this time a random number is generated to, at the same time, generate some cryptographic keys, is easy to establish an interval when the user was logged (and therefore being closer to the seed of the random algorithm). Is still difficult to determine accurately the exact time, but definitely not impossible: systems of huge relevance face this problem daily.</p>
<p>Recently a publish in the Android Market <a href="https://play.google.com/store/apps/details?id=enrique.chineseradicals" target="_blank">Chinese Radicals</a>, a program to learn Chinese. Since I need to choose randomly the Chinese radicals for the program, I decided to use it as a testbed to extend my experiment from 3 years ago. The approach is using some "contextualization" of the seed, and combining it with a probability density function. The explanation can get really complex, but I'll try to summarize:</p>
<ol>
<li>At a certain point of execution, I categorize the accesible memory of the thread where the software is running (size and number of memory blocks). This information is stored and modeled for the density function.</li>
<li>Afterwards, I take the UNIX time of the system. I apply a probability distribution to the model saved in the first step, and then combine it with this second point. I store the current time, and apply a first pass.</li>
<li>When the generation of the random number is finished, I determine the difference between that time and the time I stored in the step two. Since the memory information saved in the point 1 (the entropy of the system) might differ, this time will likely be different. Then I apply a second pass to generate, with more certainty, the random number.</li>
</ol>
<p>&nbsp;</p>
<p>I have released this generator as a Java .jar. You can include in any Java project (Swing, Blackberry, Android, etc). You can download it <a href="http://www.neo-tech.es/wp/wp-content/uploads/2012/03/Randomizator.jar_.zip" target="_blank">from here</a>. It works as follows:</p>
<ol>
<li>Import the jar</li>
<li>Import the class com.randomizator.Randomizator, and create an object Randomizator by using Randomizar myObject = new Randomizator();</li>
<li>Using randomizator.getInt( interval ) will return you a random number between 0 and the number provided.</li>
</ol>
<p>So far only the generation of integer values is supported. Please, check it out and let me know what you think.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=113</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Flirting with sentimental analysis: my own approach and some case-scenario applications</title>
		<link>http://www.neo-tech.es/wp/?p=99</link>
		<comments>http://www.neo-tech.es/wp/?p=99#comments</comments>
		<pubDate>Sun, 18 Mar 2012 16:03:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Artificial Intelligence]]></category>
		<category><![CDATA[Data Analysis]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[artificial intelligence]]></category>
		<category><![CDATA[sentimental analysis]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=99</guid>
		<description><![CDATA[Lately I have been interested in applying data analysis to information sources, particularly Twitter. Twitter has all the necessary features to provide an effective real time analysis: the API they provide allows us to access all the required features for analysis, and the volume of information is just huge. I strongly believe that Twitter has [...]]]></description>
			<content:encoded><![CDATA[<p>Lately I have been interested in applying data analysis to information sources, particularly Twitter. Twitter has all the necessary features to provide an effective real time analysis: the API they provide allows us to access all the required features for analysis, and the volume of information is just huge. I strongly believe that Twitter has already changed the way to perform intelligent analytics, since it just contains millions of thematic tweets that can be accessed with no limitation.</p>
<p>Since I began to write my own intelligent agent for <a href="http://www.teamliquid.net/forum/viewmessage.php?topic_id=303062" target="_blank">SCAI</a> (a tournament to develop an AI agent able to play Starcraft) I really got very interested in modeling human intelligence. So of course there is a lot to do in this field, and doesn't make any sense to list those well-known challenges still open. One of the fields that always attracted me was the <a href="http://en.wikipedia.org/wiki/Sentiment_analysis" target="_blank">Sentimental Analysis</a>. How interesting is the idea of extracting sentimental information of texts? Furthermore, this field deeps into many interesting areas of the Artificial Intelligence. </p>
<p>A few months ago I began to develop my own Sentimental Analyzer. It wasn't easy at the beginning: there are so many different approaches that the task seemed overwhelming. Since the project aimed to have a little more impact that traditional sentimental analyzers, I used a different approach: most of the ideas use keywords approach. They are pretty effective, but they do not ensure recall and it is more difficult to train the analyzer (i.e., train it automatically by using Twitter as a learning source). So I decided to use it using classifiers from different machine learning algorithms. The analyzer has been trained only for English, although it learns every day and I'm considering to expand it to some other languages (fundamentally Spanish and German)</p>
<p>And the experiment is working, and pretty accurate I would like to say. I have developed two applications to show them as testbeds for the analyzer:</p>
<ul>
<li><a href="https://play.google.com/store/apps/details?id=com.happinessobserver" target="_blank">The Happinness Observer</a>, which determines the happiness for a certain concept</li>
<li><a href="https://play.google.com/store/apps/details?id=com.citymood" target="_blank">City Mood</a>. This application determines the position of the user and throw information of the mood state of the city, based on the tweets from the people referencing it.</li>
</ul>
<p><br/><br />
Feel free to download the applications and let me know what you think.</p>
<p>Last but not least, I have decided to give free access to the API for those who are interested. The idea is quite simple: you can call to this URL:</p>
<p>http://feeling-analyzer.appspot.com/feeling_analyzer/feeling?text=I+love+this+guy</p>
<p>In the parameter text you should send the text (HTTP codified) you want to analyze. You will need to register for a key to use the application (and add it as a parameter on the same way: &key=providedKey). <a href="mailto:eenriquelopez@gmail.com">Drop me a line</a> if you need a key and I will provide you one.</p>
<p>The application return a value corresponding the the polarity of the text: 0 means negative, 2 means neutral and 4 means positive. As a further enhancement of the algorithm I'm planning to extend the polarity of the analysis to support multi-values (i.e., not just providing discret values but a full continuous range of them, from 0 to 100 for example). </p>
<p>Enrique López-Mañas</p>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=99</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How intelligent should the AI be</title>
		<link>http://www.neo-tech.es/wp/?p=94</link>
		<comments>http://www.neo-tech.es/wp/?p=94#comments</comments>
		<pubDate>Tue, 14 Feb 2012 09:13:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Miscelánea]]></category>
		<category><![CDATA[ai]]></category>
		<category><![CDATA[aiide]]></category>
		<category><![CDATA[artificial intelligence]]></category>
		<category><![CDATA[starcraft]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=94</guid>
		<description><![CDATA[Reading and thinking the last days about how to implement an intelligent system to play Starcraft, I had time to think about the implications of considering a system "intelligent". Nowadays, we can develop systems that are able to defeat human intelligence in certain genres. Some board games like chess, or most of the shooters are [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.aiai.ed.ac.uk/style/head.jpg" alt="" /></p>
<p>Reading and thinking the last days about how to implement an intelligent system to play Starcraft, I had time to think about the implications of considering a system "intelligent".</p>
<p>Nowadays, we can develop systems that are able to defeat human intelligence in certain genres. Some board games like chess, or most of the shooters are composed by a limited set of rules, that can be easily modelated and represented with different combination of techniques (an expert system, considering most of the rules for almost all the situations is a typical choice). As my colleague <a href="http://gamestalesjokers.blogspot.com/2011/01/inteligencia-artificial-o-el-arte-de.html" target="_blank">Bruno</a> points out (Spanish link),  we even have to limit the intelligence of those systems by applying "stupidifying" techniques. In one of the examples Bruno provides, we only allow the AI to attack the human player after spotting him;  leading this into very weird situations like the one exposed in the following video</p>
<p><iframe width="420" height="315" src="http://www.youtube.com/embed/2DZX-Fq5N0I" frameborder="0" allowfullscreen></iframe></p>
<p>But this does not apply in strategy games. Generally, a human player has much more capacity for analysis and improvisation in complex systems like strategy games, so the only way to empower the AI is to make it able to cheat  (whether in the form of direct benefits, or having access to information that the human player can't access in the same situation). The approach of experts systems, which has been proven to work well in most of the scenarios, can't be applied here:  implementing all the possible rules for one single (and simple) situation is an overwhelming task.  Cheating AI are a well-known aspect of Sid Meier's Civilization series;  in those games, the player must build his empire from scratch, while the computer's empire receives additional units at no cost and is freed from most resource restrictions. </p>
<p>How to solve this? Well, one idea could be to apply learning techniques. In the last <a target="_blank" href="http://www.aaai.org/Conferences/AIIDE/aiide.php" target="_blank">AIIDE</a> competition, the Berkeley team designed a special <a href="http://arstechnica.com/gaming/news/2011/01/skynet-meets-the-swarm-how-the-berkeley-overmind-won-the-2010-starcraft-ai-competition.ars/3" target="_blank">training field</a>, aptly named <a href="http://en.wikipedia.org/wiki/Valhalla" target="_blank">Valhalla</a>.  Instead of manually adjusting the parameters of the AI, they let the AI fight on its own for a high number of iterations, letting it analyzing its own results and adjusting the parameter on its most convenient way. The result can be seen in the following video:  they trained a group of mutalisks to massacre their natural counter unit, something unlikely if a human were controlling the units.</p>
<p><iframe width="560" height="315" src="http://www.youtube.com/embed/0BS8Mbqbnmk" frameborder="0" allowfullscreen></iframe></p>
<p>But we are still far away from reaching an AI that can defeat humans in complex scenarios. Although its <a href="http://en.wikipedia.org/wiki/Moore's_law#Ultimate_limits_of_the_law" target="_blank">limitations</a>, Moore's law is helping us, but we are not only facing a problem of computing capabilities. We also need to find different ways to model complex scenarios that the human mind is good at analyzing, but the computer is still behind us. And if we finally use cheating to defeat a human intelligence, we have to make it completely transparent: the user will not care as long as beating the AI is still challenging for him.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=94</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to use the Twitter API to post from Android</title>
		<link>http://www.neo-tech.es/wp/?p=91</link>
		<comments>http://www.neo-tech.es/wp/?p=91#comments</comments>
		<pubDate>Wed, 25 Jan 2012 17:04:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Aplicaciones móviles]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=91</guid>
		<description><![CDATA[Posting from Android into Twitter is one of the earliest stages of an Android developer. To keep full control over the posting process, we will use prefer primarily a pure OAuth post instead of dealing with Intents, so we can keep full control. So as a user, we could just think and conclude: the most [...]]]></description>
			<content:encoded><![CDATA[<p>Posting from Android into Twitter is one of the earliest stages of an Android developer. To keep full control over the posting process, we will use prefer primarily a pure OAuth post instead of dealing with Intents, so we can keep full control. So as a user, we could just think and conclude: the most typical way to authenticate is to pop up a window where we can identify with our user and password to give the application access to our account (not the full account though, just to post from the application!) and forget about the rest of the process. This might be a bit tricky process for newbies in Android. And of course, Twitter will eventually change their API or registration method, so we will found sometimes that our old implementation is not working anymore</p>
<p>We first need to register a Twitter App. For that purpose, we will visit the <a href="https://dev.twitter.com/" target="_blank">developer website</a> of Twitter. After login in, we will add a <a href="https://dev.twitter.com/apps" target="_blank">new application</a>. There are no special settings to be remembered, but the part of the callback URL has changed very oftenly since Twitter released their API. At the moment, if we are developing one application we only need to provide any random URL.</p>
<p>We need three main classes in our application to make it work.</p>
<p>First, TwitterApp. This will be the controller for the API:</p>
<pre class="java">&nbsp;
&nbsp;
<span style="color: #a1a100;">import java.net.MalformedURLException;</span>
<span style="color: #a1a100;">import java.net.URLDecoder;</span>
&nbsp;
<span style="color: #a1a100;">import oauth.signpost.OAuth;</span>
<span style="color: #a1a100;">import oauth.signpost.OAuthProvider;</span>
<span style="color: #a1a100;">import oauth.signpost.basic.DefaultOAuthProvider;</span>
<span style="color: #a1a100;">import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;</span>
<span style="color: #a1a100;">import oauth.signpost.commonshttp.CommonsHttpOAuthProvider;</span>
&nbsp;
<span style="color: #a1a100;">import twitter4j.Twitter;</span>
<span style="color: #a1a100;">import twitter4j.TwitterException;</span>
<span style="color: #a1a100;">import twitter4j.TwitterFactory;</span>
<span style="color: #a1a100;">import twitter4j.User;</span>
&nbsp;
<span style="color: #a1a100;">import android.os.Handler;</span>
<span style="color: #a1a100;">import android.os.Message;</span>
&nbsp;
<span style="color: #a1a100;">import android.app.ProgressDialog;</span>
<span style="color: #a1a100;">import android.content.Context;</span>
<span style="color: #a1a100;">import android.util.Log;</span>
<span style="color: #a1a100;">import android.view.Window;</span>
&nbsp;
<span style="color: #a1a100;">import java.net.URL;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TwitterApp <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Twitter mTwitter;
	<span style="color: #000000; font-weight: bold;">private</span> TwitterSession mSession;
	<span style="color: #000000; font-weight: bold;">private</span> twitter4j.<span style="color: #006600;">auth</span>.<span style="color: #006600;">AccessToken</span> mAccessToken;
	<span style="color: #000000; font-weight: bold;">private</span> CommonsHttpOAuthConsumer mHttpOauthConsumer;
	<span style="color: #000000; font-weight: bold;">private</span> OAuthProvider mHttpOauthprovider;
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> mConsumerKey;
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> mSecretKey;
	<span style="color: #000000; font-weight: bold;">private</span> ProgressDialog mProgressDlg;
	<span style="color: #000000; font-weight: bold;">private</span> TwDialogListener mListener;
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AContext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Context</span></a> context;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> CALLBACK_URL = <span style="color: #ff0000;">&quot;twitterapp://connect&quot;</span>;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> TAG = <span style="color: #ff0000;">&quot;TwitterApp&quot;</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> TwitterApp<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AContext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Context</span></a> context, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> consumerKey, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> secretKey<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">context</span>	= context;
&nbsp;
		mTwitter 		= <span style="color: #000000; font-weight: bold;">new</span> TwitterFactory<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		mSession		= <span style="color: #000000; font-weight: bold;">new</span> TwitterSession<span style="color: #66cc66;">&#40;</span>context<span style="color: #66cc66;">&#41;</span>;
		mProgressDlg	= <span style="color: #000000; font-weight: bold;">new</span> ProgressDialog<span style="color: #66cc66;">&#40;</span>context<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		mProgressDlg.<span style="color: #006600;">requestWindowFeature</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AWindow+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Window</span></a>.<span style="color: #006600;">FEATURE_NO_TITLE</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		mConsumerKey 	= consumerKey;
		mSecretKey	 	= secretKey;
&nbsp;
		mHttpOauthConsumer = <span style="color: #000000; font-weight: bold;">new</span> CommonsHttpOAuthConsumer<span style="color: #66cc66;">&#40;</span>mConsumerKey, mSecretKey<span style="color: #66cc66;">&#41;</span>;
		mHttpOauthprovider = <span style="color: #000000; font-weight: bold;">new</span> CommonsHttpOAuthProvider<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;https://api.twitter.com/oauth/request_token&quot;</span>,
				 					<span style="color: #ff0000;">&quot;https://api.twitter.com/oauth/access_token&quot;</span>,
				 					<span style="color: #ff0000;">&quot;https://api.twitter.com/oauth/authorize&quot;</span><span style="color: #66cc66;">&#41;</span>;
		mHttpOauthprovider.<span style="color: #006600;">setOAuth10a</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
		mAccessToken	= mSession.<span style="color: #006600;">getAccessToken</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		configureToken<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> setListener<span style="color: #66cc66;">&#40;</span>TwDialogListener listener<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		mListener = listener;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;deprecation&quot;</span><span style="color: #66cc66;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> configureToken<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>mAccessToken != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			mTwitter.<span style="color: #006600;">setOAuthConsumer</span><span style="color: #66cc66;">&#40;</span>mConsumerKey, mSecretKey<span style="color: #66cc66;">&#41;</span>;
&nbsp;
			mTwitter.<span style="color: #006600;">setOAuthAccessToken</span><span style="color: #66cc66;">&#40;</span>mAccessToken<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> hasAccessToken<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span>mAccessToken == <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #000000; font-weight: bold;">false</span> : <span style="color: #000000; font-weight: bold;">true</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> resetAccessToken<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>mAccessToken != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			mSession.<span style="color: #006600;">resetAccessToken</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			mAccessToken = <span style="color: #000000; font-weight: bold;">null</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> getUsername<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> mSession.<span style="color: #006600;">getUsername</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> updateStatus<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> status<span style="color: #66cc66;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
			mTwitter.<span style="color: #006600;">updateStatus</span><span style="color: #66cc66;">&#40;</span>status<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>TwitterException e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">throw</span> e;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> authorize<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		mProgressDlg.<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Initializing ...&quot;</span><span style="color: #66cc66;">&#41;</span>;
		mProgressDlg.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			@Override
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> run<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> authUrl = <span style="color: #ff0000;">&quot;&quot;</span>;
				<span style="color: #993333;">int</span> what = <span style="color: #cc66cc;">1</span>;
&nbsp;
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
					authUrl = mHttpOauthprovider.<span style="color: #006600;">retrieveRequestToken</span><span style="color: #66cc66;">&#40;</span>mHttpOauthConsumer, CALLBACK_URL<span style="color: #66cc66;">&#41;</span>;	
&nbsp;
					what = <span style="color: #cc66cc;">0</span>;
&nbsp;
					Log.<span style="color: #006600;">d</span><span style="color: #66cc66;">&#40;</span>TAG, <span style="color: #ff0000;">&quot;Request token url &quot;</span> + authUrl<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
					Log.<span style="color: #006600;">d</span><span style="color: #66cc66;">&#40;</span>TAG, <span style="color: #ff0000;">&quot;Failed to get request token&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
					e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
&nbsp;
				mHandler.<span style="color: #006600;">sendMessage</span><span style="color: #66cc66;">&#40;</span>mHandler.<span style="color: #006600;">obtainMessage</span><span style="color: #66cc66;">&#40;</span>what, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, authUrl<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>.<span style="color: #006600;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> processToken<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> callbackUrl<span style="color: #66cc66;">&#41;</span>  <span style="color: #66cc66;">&#123;</span>
		mProgressDlg.<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Finalizing ...&quot;</span><span style="color: #66cc66;">&#41;</span>;
		mProgressDlg.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> verifier = getVerifier<span style="color: #66cc66;">&#40;</span>callbackUrl<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			@Override
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> run<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #993333;">int</span> what = <span style="color: #cc66cc;">1</span>;
&nbsp;
				<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
					mHttpOauthprovider.<span style="color: #006600;">retrieveAccessToken</span><span style="color: #66cc66;">&#40;</span>mHttpOauthConsumer, verifier<span style="color: #66cc66;">&#41;</span>;
&nbsp;
					mAccessToken = <span style="color: #000000; font-weight: bold;">new</span> twitter4j.<span style="color: #006600;">auth</span>.<span style="color: #006600;">AccessToken</span><span style="color: #66cc66;">&#40;</span>mHttpOauthConsumer.<span style="color: #006600;">getToken</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, mHttpOauthConsumer.<span style="color: #006600;">getTokenSecret</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
					configureToken<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
					User user = mTwitter.<span style="color: #006600;">verifyCredentials</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			        mSession.<span style="color: #006600;">storeAccessToken</span><span style="color: #66cc66;">&#40;</span>mAccessToken, user.<span style="color: #006600;">getName</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			        what = <span style="color: #cc66cc;">0</span>;
				<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
					Log.<span style="color: #006600;">d</span><span style="color: #66cc66;">&#40;</span>TAG, <span style="color: #ff0000;">&quot;Error getting access token&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
					e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
&nbsp;
				mHandler.<span style="color: #006600;">sendMessage</span><span style="color: #66cc66;">&#40;</span>mHandler.<span style="color: #006600;">obtainMessage</span><span style="color: #66cc66;">&#40;</span>what, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>.<span style="color: #006600;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> getVerifier<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> callbackUrl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> verifier	 = <span style="color: #ff0000;">&quot;&quot;</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
			callbackUrl = callbackUrl.<span style="color: #006600;">replace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;twitterapp&quot;</span>, <span style="color: #ff0000;">&quot;http&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AURL+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">URL</span></a> url 		= <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AURL+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">URL</span></a><span style="color: #66cc66;">&#40;</span>callbackUrl<span style="color: #66cc66;">&#41;</span>;
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> query 	= url.<span style="color: #006600;">getQuery</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> array<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>	= query.<span style="color: #006600;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&amp;&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> parameter : array<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	             <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> v<span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> = parameter.<span style="color: #006600;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;=&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	             <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AURLDecoder+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">URLDecoder</span></a>.<span style="color: #006600;">decode</span><span style="color: #66cc66;">&#40;</span>v<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span>oauth.<span style="color: #006600;">signpost</span>.<span style="color: #006600;">OAuth</span>.<span style="color: #006600;">OAUTH_VERIFIER</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	            	 verifier = <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AURLDecoder+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">URLDecoder</span></a>.<span style="color: #006600;">decode</span><span style="color: #66cc66;">&#40;</span>v<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
	            	 <span style="color: #000000; font-weight: bold;">break</span>;
	             <span style="color: #66cc66;">&#125;</span>
	        <span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AMalformedURLException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">MalformedURLException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> verifier;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> showLoginDialog<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> url<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> TwDialogListener listener = <span style="color: #000000; font-weight: bold;">new</span> TwDialogListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			@Override
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onComplete<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				processToken<span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
&nbsp;
			@Override
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onError<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				mListener.<span style="color: #006600;">onError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Failed opening authorization page&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">new</span> TwitterDialog<span style="color: #66cc66;">&#40;</span>context, url, listener<span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Handler mHandler = <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		@Override
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> handleMessage<span style="color: #66cc66;">&#40;</span>Message msg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			mProgressDlg.<span style="color: #006600;">dismiss</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
			<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>msg.<span style="color: #006600;">what</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>msg.<span style="color: #006600;">arg1</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
					mListener.<span style="color: #006600;">onError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Error getting request token&quot;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">else</span>
					mListener.<span style="color: #006600;">onError</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Error getting access token&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>msg.<span style="color: #006600;">arg1</span> == <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
					showLoginDialog<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a><span style="color: #66cc66;">&#41;</span> msg.<span style="color: #006600;">obj</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">else</span>
					mListener.<span style="color: #006600;">onComplete</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
			<span style="color: #66cc66;">&#125;</span>
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> TwDialogListener <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onComplete<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> value<span style="color: #66cc66;">&#41;</span>;		
&nbsp;
		<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onError<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> value<span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The TwitterDialog class is composed by a basic WebView which loads the URL with the authentication fields:</p>
<pre class="java">&nbsp;
&nbsp;
<span style="color: #a1a100;">import android.app.Dialog;</span>
<span style="color: #a1a100;">import android.app.ProgressDialog;</span>
&nbsp;
<span style="color: #a1a100;">import android.graphics.Bitmap;</span>
<span style="color: #a1a100;">import android.graphics.Color;</span>
<span style="color: #a1a100;">import android.graphics.Typeface;</span>
<span style="color: #a1a100;">import android.graphics.drawable.Drawable;</span>
&nbsp;
<span style="color: #a1a100;">import android.os.Bundle;</span>
<span style="color: #a1a100;">import android.util.Log;</span>
<span style="color: #a1a100;">import android.content.Context;</span>
&nbsp;
<span style="color: #a1a100;">import android.view.Display;</span>
<span style="color: #a1a100;">import android.view.ViewGroup;</span>
<span style="color: #a1a100;">import android.view.Window;</span>
&nbsp;
<span style="color: #a1a100;">import android.webkit.WebView;</span>
<span style="color: #a1a100;">import android.webkit.WebViewClient;</span>
<span style="color: #a1a100;">import android.widget.FrameLayout;</span>
<span style="color: #a1a100;">import android.widget.LinearLayout;</span>
<span style="color: #a1a100;">import android.widget.TextView;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TwitterDialog <span style="color: #000000; font-weight: bold;">extends</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADialog+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Dialog</span></a> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">float</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> DIMENSIONS_LANDSCAPE = <span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">460</span>, <span style="color: #cc66cc;">260</span><span style="color: #66cc66;">&#125;</span>;
    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">float</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> DIMENSIONS_PORTRAIT = <span style="color: #66cc66;">&#123;</span><span style="color: #cc66cc;">280</span>, <span style="color: #cc66cc;">420</span><span style="color: #66cc66;">&#125;</span>;
    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> FrameLayout.<span style="color: #006600;">LayoutParams</span> FILL = <span style="color: #000000; font-weight: bold;">new</span> FrameLayout.<span style="color: #006600;">LayoutParams</span><span style="color: #66cc66;">&#40;</span>ViewGroup.<span style="color: #006600;">LayoutParams</span>.<span style="color: #006600;">FILL_PARENT</span>,
                         						ViewGroup.<span style="color: #006600;">LayoutParams</span>.<span style="color: #006600;">FILL_PARENT</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">int</span> MARGIN = <span style="color: #cc66cc;">4</span>;
    <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">int</span> PADDING = <span style="color: #cc66cc;">2</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> mUrl;
    <span style="color: #000000; font-weight: bold;">private</span> TwDialogListener mListener;
    <span style="color: #000000; font-weight: bold;">private</span> ProgressDialog mSpinner;
    <span style="color: #000000; font-weight: bold;">private</span> WebView mWebView;
    <span style="color: #000000; font-weight: bold;">private</span> LinearLayout mContent;
    <span style="color: #000000; font-weight: bold;">private</span> TextView mTitle;
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> TAG = <span style="color: #ff0000;">&quot;Twitter-WebView&quot;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> TwitterDialog<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AContext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Context</span></a> context, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> url, TwDialogListener listener<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span><span style="color: #66cc66;">&#40;</span>context<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mUrl 		= url;
        mListener 	= listener;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> onCreate<span style="color: #66cc66;">&#40;</span>Bundle savedInstanceState<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">onCreate</span><span style="color: #66cc66;">&#40;</span>savedInstanceState<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mSpinner = <span style="color: #000000; font-weight: bold;">new</span> ProgressDialog<span style="color: #66cc66;">&#40;</span>getContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mSpinner.<span style="color: #006600;">requestWindowFeature</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AWindow+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Window</span></a>.<span style="color: #006600;">FEATURE_NO_TITLE</span><span style="color: #66cc66;">&#41;</span>;
        mSpinner.<span style="color: #006600;">setMessage</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Loading...&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mContent = <span style="color: #000000; font-weight: bold;">new</span> LinearLayout<span style="color: #66cc66;">&#40;</span>getContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mContent.<span style="color: #006600;">setOrientation</span><span style="color: #66cc66;">&#40;</span>LinearLayout.<span style="color: #006600;">VERTICAL</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        setUpTitle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        setUpWebView<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        Display display 	= getWindow<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getWindowManager</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getDefaultDisplay</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #993333;">float</span> scale 	= getContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getResources</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getDisplayMetrics</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">density</span>;
        <span style="color: #993333;">float</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> dimensions 	= <span style="color: #66cc66;">&#40;</span>display.<span style="color: #006600;">getWidth</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &lt; display.<span style="color: #006600;">getHeight</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> ? DIMENSIONS_PORTRAIT : DIMENSIONS_LANDSCAPE;
&nbsp;
        addContentView<span style="color: #66cc66;">&#40;</span>mContent, <span style="color: #000000; font-weight: bold;">new</span> FrameLayout.<span style="color: #006600;">LayoutParams</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>dimensions<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span> * scale + <span style="color: #cc66cc;">0</span>.5f<span style="color: #66cc66;">&#41;</span>,
        							<span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>dimensions<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span> * scale + <span style="color: #cc66cc;">0</span>.5f<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> setUpTitle<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        requestWindowFeature<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AWindow+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Window</span></a>.<span style="color: #006600;">FEATURE_NO_TITLE</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        Drawable icon = getContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getResources</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getDrawable</span><span style="color: #66cc66;">&#40;</span>R.<span style="color: #006600;">drawable</span>.<span style="color: #006600;">twitter_icon</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mTitle = <span style="color: #000000; font-weight: bold;">new</span> TextView<span style="color: #66cc66;">&#40;</span>getContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mTitle.<span style="color: #006600;">setText</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Twitter&quot;</span><span style="color: #66cc66;">&#41;</span>;
        mTitle.<span style="color: #006600;">setTextColor</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AColor+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Color</span></a>.<span style="color: #006600;">WHITE</span><span style="color: #66cc66;">&#41;</span>;
        mTitle.<span style="color: #006600;">setTypeface</span><span style="color: #66cc66;">&#40;</span>Typeface.<span style="color: #006600;">DEFAULT_BOLD</span><span style="color: #66cc66;">&#41;</span>;
        mTitle.<span style="color: #006600;">setBackgroundColor</span><span style="color: #66cc66;">&#40;</span>0xFFbbd7e9<span style="color: #66cc66;">&#41;</span>;
        mTitle.<span style="color: #006600;">setPadding</span><span style="color: #66cc66;">&#40;</span>MARGIN + PADDING, MARGIN, MARGIN, MARGIN<span style="color: #66cc66;">&#41;</span>;
        mTitle.<span style="color: #006600;">setCompoundDrawablePadding</span><span style="color: #66cc66;">&#40;</span>MARGIN + PADDING<span style="color: #66cc66;">&#41;</span>;
        mTitle.<span style="color: #006600;">setCompoundDrawablesWithIntrinsicBounds</span><span style="color: #66cc66;">&#40;</span>icon, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span>, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mContent.<span style="color: #006600;">addView</span><span style="color: #66cc66;">&#40;</span>mTitle<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> setUpWebView<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        mWebView = <span style="color: #000000; font-weight: bold;">new</span> WebView<span style="color: #66cc66;">&#40;</span>getContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mWebView.<span style="color: #006600;">setVerticalScrollBarEnabled</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
        mWebView.<span style="color: #006600;">setHorizontalScrollBarEnabled</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
        mWebView.<span style="color: #006600;">setWebViewClient</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> TwitterWebViewClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        mWebView.<span style="color: #006600;">getSettings</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">setJavaScriptEnabled</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
        mWebView.<span style="color: #006600;">loadUrl</span><span style="color: #66cc66;">&#40;</span>mUrl<span style="color: #66cc66;">&#41;</span>;
        mWebView.<span style="color: #006600;">setLayoutParams</span><span style="color: #66cc66;">&#40;</span>FILL<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        mContent.<span style="color: #006600;">addView</span><span style="color: #66cc66;">&#40;</span>mWebView<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">class</span> TwitterWebViewClient <span style="color: #000000; font-weight: bold;">extends</span> WebViewClient <span style="color: #66cc66;">&#123;</span>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">boolean</span> shouldOverrideUrlLoading<span style="color: #66cc66;">&#40;</span>WebView view, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> url<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        	Log.<span style="color: #006600;">d</span><span style="color: #66cc66;">&#40;</span>TAG, <span style="color: #ff0000;">&quot;Redirecting URL &quot;</span> + url<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>url.<span style="color: #006600;">startsWith</span><span style="color: #66cc66;">&#40;</span>TwitterApp.<span style="color: #006600;">CALLBACK_URL</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        		mListener.<span style="color: #006600;">onComplete</span><span style="color: #66cc66;">&#40;</span>url<span style="color: #66cc66;">&#41;</span>;
&nbsp;
        		TwitterDialog.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">dismiss</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
        		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
        	<span style="color: #66cc66;">&#125;</span>  <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>url.<span style="color: #006600;">startsWith</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;authorize&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
        	<span style="color: #66cc66;">&#125;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">true</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onReceivedError<span style="color: #66cc66;">&#40;</span>WebView view, <span style="color: #993333;">int</span> errorCode, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> description, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> failingUrl<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        	Log.<span style="color: #006600;">d</span><span style="color: #66cc66;">&#40;</span>TAG, <span style="color: #ff0000;">&quot;Page error: &quot;</span> + description<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">onReceivedError</span><span style="color: #66cc66;">&#40;</span>view, errorCode, description, failingUrl<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            mListener.<span style="color: #006600;">onError</span><span style="color: #66cc66;">&#40;</span>description<span style="color: #66cc66;">&#41;</span>;
&nbsp;
            TwitterDialog.<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006600;">dismiss</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onPageStarted<span style="color: #66cc66;">&#40;</span>WebView view, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> url, Bitmap favicon<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            Log.<span style="color: #006600;">d</span><span style="color: #66cc66;">&#40;</span>TAG, <span style="color: #ff0000;">&quot;Loading URL: &quot;</span> + url<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">onPageStarted</span><span style="color: #66cc66;">&#40;</span>view, url, favicon<span style="color: #66cc66;">&#41;</span>;
            mSpinner.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onPageFinished<span style="color: #66cc66;">&#40;</span>WebView view, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> url<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">onPageFinished</span><span style="color: #66cc66;">&#40;</span>view, url<span style="color: #66cc66;">&#41;</span>;
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> title = mWebView.<span style="color: #006600;">getTitle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>title != <span style="color: #000000; font-weight: bold;">null</span> &amp;&amp; title.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> &gt; <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                mTitle.<span style="color: #006600;">setText</span><span style="color: #66cc66;">&#40;</span>title<span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
            mSpinner.<span style="color: #006600;">dismiss</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>And as we can expect, TwitterSession manages the twitter session:</p>
<pre class="java">&nbsp;
<span style="color: #a1a100;">import android.content.SharedPreferences;</span>
<span style="color: #a1a100;">import android.content.SharedPreferences.Editor;</span>
<span style="color: #a1a100;">import android.content.Context;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TwitterSession <span style="color: #66cc66;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> SharedPreferences sharedPref;
	<span style="color: #000000; font-weight: bold;">private</span> Editor editor;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> TWEET_AUTH_KEY = <span style="color: #ff0000;">&quot;auth_key&quot;</span>;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> TWEET_AUTH_SECRET_KEY = <span style="color: #ff0000;">&quot;auth_secret_key&quot;</span>;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> TWEET_USER_NAME = <span style="color: #ff0000;">&quot;user_name&quot;</span>;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> SHARED = <span style="color: #ff0000;">&quot;Twitter_Preferences&quot;</span>;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> TwitterSession<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AContext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Context</span></a> context<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		sharedPref 	  = context.<span style="color: #006600;">getSharedPreferences</span><span style="color: #66cc66;">&#40;</span>SHARED, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AContext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Context</span></a>.<span style="color: #006600;">MODE_PRIVATE</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		editor 		  = sharedPref.<span style="color: #006600;">edit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> storeAccessToken<span style="color: #66cc66;">&#40;</span>twitter4j.<span style="color: #006600;">auth</span>.<span style="color: #006600;">AccessToken</span> accessToken, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> username<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		editor.<span style="color: #006600;">putString</span><span style="color: #66cc66;">&#40;</span>TWEET_AUTH_KEY, accessToken.<span style="color: #006600;">getToken</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		editor.<span style="color: #006600;">putString</span><span style="color: #66cc66;">&#40;</span>TWEET_AUTH_SECRET_KEY, accessToken.<span style="color: #006600;">getTokenSecret</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		editor.<span style="color: #006600;">putString</span><span style="color: #66cc66;">&#40;</span>TWEET_USER_NAME, username<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		editor.<span style="color: #006600;">commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> resetAccessToken<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		editor.<span style="color: #006600;">putString</span><span style="color: #66cc66;">&#40;</span>TWEET_AUTH_KEY, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
		editor.<span style="color: #006600;">putString</span><span style="color: #66cc66;">&#40;</span>TWEET_AUTH_SECRET_KEY, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
		editor.<span style="color: #006600;">putString</span><span style="color: #66cc66;">&#40;</span>TWEET_USER_NAME, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		editor.<span style="color: #006600;">commit</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> getUsername<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> sharedPref.<span style="color: #006600;">getString</span><span style="color: #66cc66;">&#40;</span>TWEET_USER_NAME, <span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> twitter4j.<span style="color: #006600;">auth</span>.<span style="color: #006600;">AccessToken</span> getAccessToken<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> token 		= sharedPref.<span style="color: #006600;">getString</span><span style="color: #66cc66;">&#40;</span>TWEET_AUTH_KEY, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> tokenSecret 	= sharedPref.<span style="color: #006600;">getString</span><span style="color: #66cc66;">&#40;</span>TWEET_AUTH_SECRET_KEY, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>token != <span style="color: #000000; font-weight: bold;">null</span> &amp;&amp; tokenSecret != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> twitter4j.<span style="color: #006600;">auth</span>.<span style="color: #006600;">AccessToken</span><span style="color: #66cc66;">&#40;</span>token, tokenSecret<span style="color: #66cc66;">&#41;</span>;
		<span style="color: #b1b100;">else</span>
			<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">null</span>;
	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Now let's see how this works. In this application we will need to insert our consumer and private keys in the upper variables. The rest of the code is easy to understand: we will initialize the Twitter object, and we will post!</p>
<pre class="java">&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ProceedActivity <span style="color: #000000; font-weight: bold;">extends</span> Activity <span style="color: #66cc66;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> TwitterApp mTwitter;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> twitter_consumer_key = <span style="color: #ff0000;">&quot;key&quot;</span>;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> twitter_secret_key = <span style="color: #ff0000;">&quot;key&quot;</span>;
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> username = <span style="color: #ff0000;">&quot;&quot;</span>;
	<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADialog+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Dialog</span></a> dialog ;
&nbsp;
	 <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> TwDialogListener mTwLoginDialogListener = <span style="color: #000000; font-weight: bold;">new</span> TwDialogListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	        @Override
	        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onComplete<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	            username    = mTwitter.<span style="color: #006600;">getUsername</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	            username    = <span style="color: #66cc66;">&#40;</span>username.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #ff0000;">&quot;No Name&quot;</span> : username;
	            Toast.<span style="color: #006600;">makeText</span><span style="color: #66cc66;">&#40;</span>getActivity<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;Connected to Twitter as &quot;</span> + username, Toast.<span style="color: #006600;">LENGTH_LONG</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	            postToTwitter<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;texto&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
	        <span style="color: #66cc66;">&#125;</span>
&nbsp;
	        @Override
	        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onError<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> value<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
	        	Toast.<span style="color: #006600;">makeText</span><span style="color: #66cc66;">&#40;</span>getBaseContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;Twitter connection failed&quot;</span>, Toast.<span style="color: #006600;">LENGTH_LONG</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
	        <span style="color: #66cc66;">&#125;</span>
	    <span style="color: #66cc66;">&#125;</span>;
	@Override
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #993333;">void</span> onCreate<span style="color: #66cc66;">&#40;</span>Bundle savedInstanceState<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006600;">onCreate</span><span style="color: #66cc66;">&#40;</span>savedInstanceState<span style="color: #66cc66;">&#41;</span>;
		setContentView<span style="color: #66cc66;">&#40;</span>R.<span style="color: #006600;">layout</span>.<span style="color: #006600;">proceed</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		mTwitter = <span style="color: #000000; font-weight: bold;">new</span> TwitterApp<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, twitter_consumer_key,twitter_secret_key<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		mTwitter.<span style="color: #006600;">setListener</span><span style="color: #66cc66;">&#40;</span>mTwLoginDialogListener<span style="color: #66cc66;">&#41;</span>;
&nbsp;
		dialog = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADialog+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Dialog</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span>;
		dialog.<span style="color: #006600;">setContentView</span><span style="color: #66cc66;">&#40;</span>R.<span style="color: #006600;">layout</span>.<span style="color: #006600;">custom_dialog</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
					ImageView imageTwitter = <span style="color: #66cc66;">&#40;</span>ImageView<span style="color: #66cc66;">&#41;</span> dialog.<span style="color: #006600;">findViewById</span><span style="color: #66cc66;">&#40;</span>R.<span style="color: #006600;">id</span>.<span style="color: #006600;">imageTwitter</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
					 imageTwitter.<span style="color: #006600;">setOnClickListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> OnClickListener<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
						@Override
						<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onClick<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AView+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">View</span></a> v<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
							ProgressDialog dialog = ProgressDialog.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span>getBaseContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #ff0000;">&quot;&quot;</span>,
			                        <span style="color: #ff0000;">&quot;Loading. Please wait...&quot;</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
							<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>mTwitter.<span style="color: #006600;">hasAccessToken</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
								postToTwitter<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a>.<span style="color: #006600;">valueOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;texto&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
							<span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
								mTwitter.<span style="color: #006600;">authorize</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
							<span style="color: #66cc66;">&#125;</span>
							dialog.<span style="color: #006600;">dismiss</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
						<span style="color: #66cc66;">&#125;</span>
&nbsp;
					 <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> postToTwitter<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> review<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AThread+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Thread</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            @Override
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> run<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #993333;">int</span> what = <span style="color: #cc66cc;">0</span>;
&nbsp;
                <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
                    mTwitter.<span style="color: #006600;">updateStatus</span><span style="color: #66cc66;">&#40;</span>getResources<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getString</span><span style="color: #66cc66;">&#40;</span>R.<span style="color: #006600;">string</span>.<span style="color: #006600;">finalTextShare</span><span style="color: #66cc66;">&#41;</span>+<span style="color: #ff0000;">&quot; &quot;</span>+review<span style="color: #66cc66;">&#41;</span>;
                <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                    what = <span style="color: #cc66cc;">1</span>;
                <span style="color: #66cc66;">&#125;</span>
&nbsp;
                mHandler.<span style="color: #006600;">sendMessage</span><span style="color: #66cc66;">&#40;</span>mHandler.<span style="color: #006600;">obtainMessage</span><span style="color: #66cc66;">&#40;</span>what<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
            <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>.<span style="color: #006600;">start</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Handler mHandler = <span style="color: #000000; font-weight: bold;">new</span> Handler<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        @Override
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> handleMessage<span style="color: #66cc66;">&#40;</span>Message msg<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> text = <span style="color: #66cc66;">&#40;</span>msg.<span style="color: #006600;">what</span> == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #ff0000;">&quot;Posted to Twitter&quot;</span> : <span style="color: #ff0000;">&quot;Post to Twitter failed&quot;</span>;
            dialog.<span style="color: #006600;">dismiss</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
            Toast.<span style="color: #006600;">makeText</span><span style="color: #66cc66;">&#40;</span>getBaseContext<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, text, Toast.<span style="color: #006600;">LENGTH_SHORT</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">show</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Activity getActivity <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>Enrique López-Mañas</p>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=91</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Push: Client (Android based) and server</title>
		<link>http://www.neo-tech.es/wp/?p=77</link>
		<comments>http://www.neo-tech.es/wp/?p=77#comments</comments>
		<pubDate>Fri, 06 Jan 2012 20:46:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[c2dm]]></category>
		<category><![CDATA[push]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=77</guid>
		<description><![CDATA[As part of the training and pushing the boundaries in my department, we recently experimented with Push technologies and their application to mobile development. Whereas iPhone seems to support natively push messaging, we soon realize that Android was not perfect in this direction. Surprisingly they haven't yet considered that a native push support is a [...]]]></description>
			<content:encoded><![CDATA[<p>As part of the training and pushing the boundaries in my department, we recently experimented with Push technologies and their application to mobile development. Whereas iPhone seems to support natively push messaging, we soon realize that Android was not perfect in this direction. Surprisingly they haven't yet considered that a native push support is a technology worth to embedded within their <a href="http://developer.android.com/index.html" target="_blank">official SDK</a>, and this is a completely setback for developers aiming to develop and create their own ideas.</p>
<p>&nbsp;</p>
<p>Let's explain this is in shortly: push is the name given to the fact that messages can be sent to the receiver, instead of being requested from the client. Although the concept might sounds easy to understand, the explanation on why the implementation is hard is a bit more complex: we can summarize it by saying that the protocol running under the modern Internet communications (TCP/IP) was not designed for Push technologies. For more information on this topic, we recommend to read the <a href="http://www.onsip.com/blog/mike/2010/07/07/the-dangers-of-push-notification-in-mobile-sip-applications" target="_blank">following article</a>.</p>
<p>&nbsp;</p>
<p>So there are three general solutions widely accepted to implement our own Push notifications:</p>
<ul>
<li><a href="http://code.google.com/intl/de-DE/android/c2dm/" target="_blank">Google C2DM</a>: recently released by Google,</li>
<li>Poll: although it is not a real push request, the effect looks the same: we periodically poll the server looking for new data. The more often we poll the closer we get to the real-time push. But obviously, it will never be on real-time. Plus the battery will die very quickly</li>
<li>Persistent TCP/IP connection: the device initiates a long-lived mostly idle TCP/IP connection by playing with the feature of "keep-alive" (sending occasionally messages to the devices). Whenever something new is on the server, the phone initiates a fully TCP connection to acquire the new data. This is the underlying technology for Google C2DM, but you might want to have full control on the entire process until C2DM provides a reliable service.</li>
</ul>
<p>&nbsp;</p>
<div>The Google C2DM API is well described on the official service, but we might need to know a bit more about how to set up the server part. In short, we will need three parts: the C2DM server itself, a client implementation of Push (i.e., in your Android device) and a third party application service. This last component might be a bit tricky to understand, but a short summary of the functionalities performed are:</div>
<p>&nbsp;</p>
<div>
<ul>
<li>Is able to communicate with the client.</li>
<li>Will fire HTTP requests to the C2DM server.</li>
<li>Handle requests algorithm (for instance, we could design it for performing an exponential back off).</li>
<li>Storages the authentication tokens. They are need to handle applications with a little bit of complexity</li>
</ul>
<p>&nbsp;</p>
<div>As stated before, is easy to set up all the parts, but we might spend more time thinking about the third party server implementation. And that's the sugar of this article.</div>
<p>&nbsp;</p>
<div>According to the source site of C2DM, the implementation of the Android is quite trivial. We first need to declare in our Manifest the required permissions for the application, which are com.google.android.c2dm.permission.RECEIVE, com.example.myapp.permission.C2D_MESSAGE and of course android.permission.INTERNET.  We also need to declare our receivers. For further information, check out the <a title="Android Manifest for C2DM" href="http://code.google.com/intl/de-DE/android/c2dm/#manifest">official link</a>.</div>
<div>The next step is to register on our application, what can be done with the following code. Typically, we will add it into an onCreate method, or when the application needs to prepare itself for the push.</div>
<p>&nbsp;</p>
<pre class="java">Intent registrationIntent = <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;com.google.android.c2dm.intent.REGISTER&quot;</span><span style="color: #66cc66;">&#41;</span>;
registrationIntent.<span style="color: #006600;">putExtra</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;app&quot;</span>, PendingIntent.<span style="color: #006600;">getBroadcast</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">// boilerplate</span>
registrationIntent.<span style="color: #006600;">putExtra</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;sender&quot;</span>, emailOfSender<span style="color: #66cc66;">&#41;</span>;
startService<span style="color: #66cc66;">&#40;</span>registrationIntent<span style="color: #66cc66;">&#41;</span>;</pre>
<p>&nbsp;<br />
Unregistering is also trivial (again, we might want to add this into the onDestroy event):<br />
&nbsp;</p>
<pre class="java">&nbsp;
Intent unregIntent = <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;com.google.android.c2dm.intent.UNREGISTER&quot;</span><span style="color: #66cc66;">&#41;</span>;
unregIntent.<span style="color: #006600;">putExtra</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;app&quot;</span>, PendingIntent.<span style="color: #006600;">getBroadcast</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
startService<span style="color: #66cc66;">&#40;</span>unregIntent<span style="color: #66cc66;">&#41;</span>;</pre>
<p>&nbsp;<br />
The basic part for handling the message needs a bit more of explanation. When the method onReceive of our BroadcastReceiver is triggered, we might need to check if we are dealing with the registration or if we are just receiving a push notification. We provide the code for the first case. For the second one, we might need to create our own method based on our design. We will be able to receive a message through the Intent. And that's the part we need to handle on the server side.<br />
&nbsp;</p>
<pre class="java"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> onReceive<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AContext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Context</span></a> context, Intent intent<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>intent.<span style="color: #006600;">getAction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;com.google.android.c2dm.intent.REGISTRATION&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      handleRegistration<span style="color: #66cc66;">&#40;</span>context, intent<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>intent.<span style="color: #006600;">getAction</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">equals</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;com.google.android.c2dm.intent.RECEIVE&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      handleMessage<span style="color: #66cc66;">&#40;</span>context, intent<span style="color: #66cc66;">&#41;</span>;
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> handleRegistration<span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AContext+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Context</span></a> context, Intent intent<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> registration = intent.<span style="color: #006600;">getStringExtra</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;registration_id&quot;</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>intent.<span style="color: #006600;">getStringExtra</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;error&quot;</span><span style="color: #66cc66;">&#41;</span> != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">// Registration failed, should try again later.</span>
   <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>intent.<span style="color: #006600;">getStringExtra</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;unregistered&quot;</span><span style="color: #66cc66;">&#41;</span> != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">// unregistration done, new messages from the authorized sender will be rejected</span>
   <span style="color: #66cc66;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>registration != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #808080; font-style: italic;">// Send the registration ID to the 3rd party site that is sending the messages.</span>
      <span style="color: #808080; font-style: italic;">// This should be done in a separate thread.</span>
      <span style="color: #808080; font-style: italic;">// When done, remember that all registration is done.</span>
   <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>&nbsp;</p>
<p>So this is the core of the server application. This method needs to receive an auth code, and the device registration ID. The device registration ID will be provided when our phone is registered in the previous step. The authcode is the authentication through a Google account. For instance, we could create a dummy account, and send it along with the push notification:</p>
<p>&nbsp;</p>
<pre class="php">googleAuthenticate<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;mydummyaccount@gmail.com&quot;</span>,<span style="color: #ff0000;">&quot;mydummypassword&quot;</span><span style="color: #66cc66;">&#41;</span></pre>
<p>&nbsp;</p>
<p>Afterwards, we can send a message type and the content itself. This allows us to have more flexibility in our notifications. For instance, we might provide a message type which corresponds with an error, and on the content we can send the complete error text.<br />
&nbsp;</p>
<pre class="php"><span style="color: #000000; font-weight: bold;">function</span> sendMessageToPhone<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$authCode</span>, <span style="color: #0000ff;">$deviceRegistrationId</span>, <span style="color: #0000ff;">$msgType</span>, <span style="color: #0000ff;">$messageText</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #0000ff;">$headers</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Authorization: GoogleLogin auth='</span> . <span style="color: #0000ff;">$authCode</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #0000ff;">$data</span> = <a href="http://www.php.net/array"><span style="color: #000066;">array</span></a><span style="color: #66cc66;">&#40;</span>
      <span style="color: #ff0000;">'registration_id'</span> =&amp;gt; <span style="color: #0000ff;">$deviceRegistrationId</span>,
      <span style="color: #ff0000;">'collapse_key'</span> =&amp;gt; <span style="color: #0000ff;">$msgType</span>,
      <span style="color: #ff0000;">'data.message'</span> =&amp;gt; <span style="color: #0000ff;">$messageText</span> <span style="color: #808080; font-style: italic;">//TODO Add more params with just simple data instead</span>
   <span style="color: #66cc66;">&#41;</span>;
&nbsp;
   <span style="color: #0000ff;">$ch</span> = curl_init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
   curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_URL, <span style="color: #ff0000;">&quot;https://android.apis.google.com/c2dm/send&quot;</span><span style="color: #66cc66;">&#41;</span>;
   <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">&#41;</span>
      curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_HTTPHEADER, <span style="color: #0000ff;">$headers</span><span style="color: #66cc66;">&#41;</span>;
      curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
      curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_POST, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
      curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
      curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_POSTFIELDS, <span style="color: #0000ff;">$data</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #0000ff;">$response</span> = curl_exec<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      curl_close<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$response</span>;
   <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> googleAuthenticate<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$username</span>, <span style="color: #0000ff;">$password</span>, <span style="color: #0000ff;">$source</span>=<span style="color: #ff0000;">&quot;Company-AppName-Version&quot;</span>, <span style="color: #0000ff;">$service</span>=<span style="color: #ff0000;">&quot;ac2dm&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
   <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span> <a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'google_auth_id'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;amp;&amp;amp; <span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'google_auth_id'</span><span style="color: #66cc66;">&#93;</span> != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'google_auth_id'</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// get an authorization token</span>
<span style="color: #0000ff;">$ch</span> = curl_init<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span>!ch<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_URL, <span style="color: #ff0000;">&quot;https://www.google.com/accounts/ClientLogin&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #0000ff;">$post_fields</span> = <span style="color: #ff0000;">&quot;accountType=&quot;</span> . <a href="http://www.php.net/urlencode"><span style="color: #000066;">urlencode</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'HOSTED_OR_GOOGLE'</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
. <span style="color: #ff0000;">&quot;&amp;amp;Email=&quot;</span> . <a href="http://www.php.net/urlencode"><span style="color: #000066;">urlencode</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$username</span><span style="color: #66cc66;">&#41;</span>
. <span style="color: #ff0000;">&quot;&amp;amp;Passwd=&quot;</span> . <a href="http://www.php.net/urlencode"><span style="color: #000066;">urlencode</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$password</span><span style="color: #66cc66;">&#41;</span>
. <span style="color: #ff0000;">&quot;&amp;amp;source=&quot;</span> . <a href="http://www.php.net/urlencode"><span style="color: #000066;">urlencode</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$source</span><span style="color: #66cc66;">&#41;</span>
. <span style="color: #ff0000;">&quot;&amp;amp;service=&quot;</span> . <a href="http://www.php.net/urlencode"><span style="color: #000066;">urlencode</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$service</span><span style="color: #66cc66;">&#41;</span>;
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_HEADER, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_POST, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_POSTFIELDS, <span style="color: #0000ff;">$post_fields</span><span style="color: #66cc66;">&#41;</span>;
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_RETURNTRANSFER, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_FRESH_CONNECT, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_HTTPAUTH, CURLAUTH_ANY<span style="color: #66cc66;">&#41;</span>;
curl_setopt<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span>, CURLOPT_SSL_VERIFYPEER, <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">// for debugging the request</span>
<span style="color: #808080; font-style: italic;">//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request</span>
&nbsp;
<span style="color: #0000ff;">$response</span> = curl_exec<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;">//var_dump(curl_getinfo($ch)); //for debugging the request</span>
<span style="color: #808080; font-style: italic;">//var_dump($response);</span>
&nbsp;
<span style="color: #808080; font-style: italic;">//var_dump(curl_getinfo($ch)); //for debugging the request</span>
<span style="color: #808080; font-style: italic;">//var_dump($response);</span>
&nbsp;
curl_close<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ch</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/strpos"><span style="color: #000066;">strpos</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$response</span>, <span style="color: #ff0000;">'200 OK'</span><span style="color: #66cc66;">&#41;</span> === <span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// find the auth code</span>
<a href="http://www.php.net/preg_match"><span style="color: #000066;">preg_match</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;/(Auth=)([<span style="color: #000099; font-weight: bold;">\w</span>|-]+)/&quot;</span>, <span style="color: #0000ff;">$response</span>, <span style="color: #0000ff;">$matches</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!<span style="color: #0000ff;">$matches</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
   <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span>;
<span style="color: #66cc66;">&#125;</span>
<span style="color: #0000ff;">$_SESSION</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'google_auth_id'</span><span style="color: #66cc66;">&#93;</span> = <span style="color: #0000ff;">$matches</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">$matches</span><span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #66cc66;">&#125;</span></pre>
<p>&nbsp;</p>
<p>I hope you enjoyed the tutorial. For any further questions or inquiries, you can drop me a line into my personal <a href="mailto:eenriquelopez@gmail.com" target="_blank">email</a>.</p>
<p>&nbsp;</p>
<p>Enrique López-Mañas</p>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=77</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New automatic language translation tool for Android</title>
		<link>http://www.neo-tech.es/wp/?p=71</link>
		<comments>http://www.neo-tech.es/wp/?p=71#comments</comments>
		<pubDate>Mon, 26 Dec 2011 17:48:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[languages]]></category>
		<category><![CDATA[google translate]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[translations]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=71</guid>
		<description><![CDATA[Since I have been working in Barcelona, I got so much in touch with Android and mobile developers than in Germany, since my work there was a little bit more theoretical rather than applied. Although I don't use on a regular basis Google Translate or any engine to translate my applications (this is a problem [...]]]></description>
			<content:encoded><![CDATA[<p>Since I have been working in Barcelona, I got so much in touch with Android and mobile developers than in Germany, since my work there was a little bit more theoretical rather than applied.</p>
<p>Although I don't use on a regular basis <a href="http://translate.google.com/" target="_blank">Google Translate</a> or any engine to translate my applications (this is a problem of quality vs. quantity, where I bet quality should prevail), I realized that many developers of successful applications are using Google Translate to generate new string files of their projects. And it is, in fact, much more widespread of what I thought. When I have been talking with those developers, they confess after one or two beers that they use web powered engines (i.e., Google Translate) and they usually don't perform a pair check or a professional review of their generated tokens. Without judging this behavior, I was interested in the procedure, and the first question to arise was: have you automatized the process? The most recurrent answer was a "No", while they began to stare at the floor.</p>
<p>I thought it could be interesting for some projects, so I thought I could expand the translation tool I published a <a title="Language Assistant program for Android" href="http://www.neo-tech.es/wp/?p=49" target="_blank">few months ago</a>. Why couldn't we just perform the translation systematically? And that's why I'm presenting the second version of the tool.</p>
<p>The <a href="http://code.google.com/intl/de-DE/apis/language/translate/overview.html" target="_blank">Google Translate API</a> is not free, and since I don't want to offer my API key and see a huge bill at the end of the month in my bank account, I'm offering two different versions of the tool: the first version allows you to insert your own key, and perform the translation. And if you don't want to deal with the registration and Google Checkout, you can get a paid version which uses my own API key for only 5$. So if you think this tool can be helpful for you, those 5$ will make you save a lot of time and I will get a few extra beers and some support to develop projects on my free time. As shown on the following screen, you just need to type your API key on the following edit text.</p>
<p><a href="http://www.neo-tech.es/wp/wp-content/uploads/2011/12/Bildschirmfoto-2011-12-26-um-18.43.12.png"><img class="alignnone size-medium wp-image-73" title="Bildschirmfoto 2011-12-26 um 18.43.12" src="http://www.neo-tech.es/wp/wp-content/uploads/2011/12/Bildschirmfoto-2011-12-26-um-18.43.12-300x247.png" alt="" width="300" height="247" /></a></p>
<p>There are still some upcoming tasks to be performed, but this can be considered as a working alpha version.</p>
<p>This tool will not be released as GPL software as the previous one. However, if you need the source code for any purpose you always can send me an email in order to convince me.</p>
<p>The free version can be download from <a href="http://code.google.com/p/language-assistant-android/downloads/list" target="_blank">here</a> (version 2.0). If you want the paid version, feel free to drop me <a href="mailto:eenriquelopez@gmail.com">a line</a> or contact me via <a href="http://twitter.com/#!/eenriquelopez" target="_blank">Twitter</a> or <a href="http://www.facebook.com/#!/eenriquelopez" target="_blank">Facebook</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=71</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First session of idkLabs</title>
		<link>http://www.neo-tech.es/wp/?p=67</link>
		<comments>http://www.neo-tech.es/wp/?p=67#comments</comments>
		<pubDate>Fri, 18 Nov 2011 11:03:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Miscelánea]]></category>
		<category><![CDATA[brainstorming]]></category>
		<category><![CDATA[idklabs]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=67</guid>
		<description><![CDATA[Last Wednesday we inaugurated the IdkLabs at our office. The underlying philosophy is to create a space of coworking and to put in practice our own ideas and personal projects. It is a real injection of fresh air to work in more creative topics, and to forget a bit of the mechanic, less pleasant development. [...]]]></description>
			<content:encoded><![CDATA[<p>Last Wednesday we inaugurated the IdkLabs at our office. The underlying philosophy is to create a space of coworking and to put in practice our own ideas and personal projects. It is a real injection of fresh air to work in more creative topics, and to forget a bit of the mechanic, less pleasant development. Plus the result of working in a funny, fresh environment with talented people can only result in good ideas' conceptions.</p>
<p>&nbsp;</p>
<p>There are several proposed models of <a title="Brainstorming" href="http://en.wikipedia.org/wiki/Brainstorming" target="_blank">BrainStorming</a>, and we decided to experiment with a structured (also called circular) variation of BrainStorming: during all day long each person with a proposal should send it anonymously to the BrainStorming Master. The proposals could not be longer than a couple of sentences.</p>
<p>&nbsp;</p>
<p>At the beginning of the BrainStorming, all the proposals were distributed among the participants. From that point on, there wasn't any limitation for the number of proposals per person, and they were just voluntary, we could have more or less proposals than people. In this case we had 7 proposals and 18 people. So we distributed each proposal trying to be as symmetrical as possible (i.e., keeping the same distance between each person with a proposal).</p>
<p>&nbsp;</p>
<p><a href="http://blog.ideaknow.com/wp-content/uploads/2011/10/Bildschirmfoto-2011-10-20-um-22.53.351.png"><img class="aligncenter" title="Structured Brainstorming" src="http://blog.ideaknow.com/wp-content/uploads/2011/10/Bildschirmfoto-2011-10-20-um-22.53.351.png" alt="" width="453" height="459" /></a></p>
<p>&nbsp;</p>
<p>In one minute time, each person reads the proposal and jotts down in the paper any idea, and after the minute they should give the piece of paper to the person sitting to their right. Eventually, they would receive another piece of paper from their left side, and the process continues until all the people have written in all the pieces of paper. Although this might sound like a little game, it really helped us to gain some feedback and new ideas, and to get into a funny atmosphere. When everything was over, the owner of each proposal had up to 5 minutes to explain it, and to convince the unconvinced or the people with no proposal that they should join their project. By this time, it is likely that the group will have extensively elaborated on each idea.</p>
<p>&nbsp;</p>
<p><a href="http://blog.ideaknow.com/wp-content/uploads/2011/10/Bildschirmfoto-2011-10-20-um-22.53.351.png"><br />
</a></p>
<p><a href="http://blog.ideaknow.com/wp-content/uploads/2011/10/Bildschirmfoto-2011-10-20-um-22.58.44.png"><img class="aligncenter" title="Presentation" src="http://blog.ideaknow.com/wp-content/uploads/2011/10/Bildschirmfoto-2011-10-20-um-22.58.44.png" alt="" width="415" height="438" /></a></p>
<p>&nbsp;</p>
<p>What came next was a free space of creation. We talked about the projects, about ideas to expand them or about any topic we wanted, but everyone kept the discussed projects in mind. Some of us moved our computers into the meeting room, so we could work and talk together. Although we didn't have a lot of time to code or begin with the projects, we began to draft and collect our thoughts. A few warriors stayed until almost 23:00, so we can consider this event as being widely accepted in its first edition.</p>
<p>&nbsp;</p>
<p>We aim to keep this meeting bi-weekly, although considering the scope of the proposals it might change to weekly. More reports to come!</p>
<p>&nbsp;</p>
<p>Oh yes, you might be wondering what happens with the projects we began to work on. Although we didn't sign any NDA, we would like to talk about them in future posts, so remember to add the website to Google Reader or your favourite RSS reader <img src='http://www.neo-tech.es/wp/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>&nbsp;</p>
<p><a href="http://blog.ideaknow.com/wp-content/uploads/2011/10/AcIeGUzCIAA2wEp.jpg"><img class="aligncenter" title="AcIeGUzCIAA2wEp" src="http://blog.ideaknow.com/wp-content/uploads/2011/10/AcIeGUzCIAA2wEp-300x224.jpg" alt="" width="300" height="224" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=67</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>cvBlob library for Android</title>
		<link>http://www.neo-tech.es/wp/?p=53</link>
		<comments>http://www.neo-tech.es/wp/?p=53#comments</comments>
		<pubDate>Sun, 09 Oct 2011 20:04:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Algoritmos]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Computer vision]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[sudoku]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=53</guid>
		<description><![CDATA[I recently moved to Barcelona to start working in here. Although I'm not working in any computer-vision based project, I still keep a high interest in this field, trying to conduct as many personal projects as possible. My work team is highly motivated and full of professionals, and we all keep our personal projects besides [...]]]></description>
			<content:encoded><![CDATA[<p>I recently moved to Barcelona to start working in here. Although I'm not working in any computer-vision based project, I still keep a high interest in this field, trying to conduct as many personal projects as possible. My work team is highly motivated and full of professionals, and we all keep our personal projects besides our work.</p>
<p>Recently I met <a title="Fegabe" href="https://plus.google.com/115987041616851932912/posts" target="_blank">Fegabe</a> in Barcelona, who's a member of the <a title="Barcelona gtug" href="http://barcelona.gtugs.org/" target="_blank">GTUG Barcelona</a> core team and an Android Developer. We decided to start together a Sudoku Solver for Android. Although there are already many of them published in the Market, we just wanted to do it for fun and to get a bit deeper into computer vision and pattern recognition. There is an <a title="OpenCV for Android" href="http://opencv.willowgarage.com/wiki/Android" target="_blank">OpenCV port</a> for Android, but we decided to keep our implementation pure Java.</p>
<p>The following steps are applied in order to capture, detect and solve the Sudoku:</p>
<p>-The user must take a picture of the Sudoku using the camera of the device.</p>
<p>-After the picture is taken, a <a title="Thresholding" href="http://en.wikipedia.org/wiki/Thresholding_(image_processing)" target="_blank">Threshold</a> is applied to the image, so we can get a black and white version. Of course, there is some preprocessing involved. Things like smoothing out noise, some morphological operations, etc.</p>
<p>-When the picture has been taken, we apply a <a title="Blob detection" href="http://en.wikipedia.org/wiki/Blob_detection" target="_blank">blob detection</a> in order to detect the biggest segment of the image. We work under the assumption that this segment will be the outer line of the Sudoku table.</p>
<p>-Afterwards, we can use the <a href="http://www.aishack.in/2010/03/the-hough-transform/">Hough transform</a> to get lines in the image. It returns lines in mathematical terms. So after this step, we’ll know exactly where a lines lies… and not just the pixels where it lies.</p>
<p>-When the lines has been detected, it will be easier to locate each individual cell. The number inside will be recognized using a <a title="Neural Network" href="http://en.wikipedia.org/wiki/Neural_network" target="_blank">neural network</a>.</p>
<p>-The last step is the easiest: we just solve the sudoku by using any of all the <a title="Sudoku algorithms" href="http://en.wikipedia.org/wiki/Sudoku_algorithms" target="_blank">known algorithms</a></p>
<div>The Sudoku Solver is still to be finished, but the cvBlob library is working fine, so I decided to publish it. The implementation is based on Dr. <a title="Andrew Greensted" href="http://www.elec.york.ac.uk/staff/ajg112.html" target="_blank">Andrew Greensted</a> suggestion for <a title="Blob Detection" href="http://www.labbookpages.co.uk/software/imgProc/blobDetection.html" target="_blank">Blob Detection</a>.</div>
<p>I shared the project with a GPL license in <a href="http://code.google.com/p/cvblob-for-android/" target="_blank">Google Code</a>. All the technical information can be found there, besides the source code and a binary file for Android. If you wanna try out, I would highly appreciate any feedback</p>
<p>&nbsp;</p>
<p><img src="http://www.neo-tech.es/img/cvBlobDetection.png" alt="blob detection" width="255" /></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=53</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using PHP on server side to generate JSON</title>
		<link>http://www.neo-tech.es/wp/?p=51</link>
		<comments>http://www.neo-tech.es/wp/?p=51#comments</comments>
		<pubDate>Sat, 30 Jul 2011 10:54:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[client side]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[server side]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=51</guid>
		<description><![CDATA[Recently, I publish one application into the Android Market that tries to predict when Spain will default. The application uses the data provided by my colleague Juan Carlos Barba from his server. There are basically a set of levels pointing out the seriousness of the Spanish level of CDS and Spread of the debt. The model establishes 5 different levels of alert [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I publish one <a href="goo.gl/DPsAL" title="DefCon - Android Market" target="_blank">application</a> into the Android Market that tries to predict when Spain will default. The application uses the data provided by my colleague Juan Carlos Barba from his <a href="jcbcarc.dyndns.org/Defcon.php" target="_blank">server</a>. There are basically a set of levels pointing out the seriousness of the Spanish level of <a href="http://en.wikipedia.org/wiki/Credit_default_swap" title="CDS" target="_blank">CDS</a> and <a href="http://en.wikipedia.org/wiki/Credit_default_swap" title="spread" target="_blank">Spread </a>of the debt. The model establishes 5 different levels of alert (or how he called them, DefCon). My implementation customized the data to visualize into the Android Platform.</p>
<p>Firstly, I needed to access his database into the server, which was a slow task due to the nature of the server (a personal computer with a normal DSL connection). My idea was to rely this task into something that could avoid overloading the server, and then I thought about everything that happens now with all the client / server application. Why not using a PHP file to generate JSON with the information I needed?</p>
<p>Since I do not have access to Juan Carlos' server, I decided to do it on my own server. I created some PHP files that query the server, and give me the information in JSON format</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<a href="http://www.php.net/mysql_connect"><span style="color: #000066;">mysql_connect</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;jcbcarc.dyndns.org&quot;</span>,<span style="color: #ff0000;">&quot;user&quot;</span>,<span style="color: #ff0000;">&quot;password&quot;</span><span style="color: #66cc66;">&#41;</span>;
<a href="http://www.php.net/mysql_select_db"><span style="color: #000066;">mysql_select_db</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;economia&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">$q</span>=<a href="http://www.php.net/mysql_query"><span style="color: #000066;">mysql_query</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;SELECT * , DATE(FechayHora) AS fecha FROM economia.spreads GROUP BY fecha ORDER BY fecha;&quot;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$e</span>=<a href="http://www.php.net/mysql_fetch_assoc"><span style="color: #000066;">mysql_fetch_assoc</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$q</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #0000ff;">$output</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>=<span style="color: #0000ff;">$e</span>;
&nbsp;
<a href="http://www.php.net/print"><span style="color: #000066;">print</span></a><span style="color: #66cc66;">&#40;</span>json_encode<span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$output</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<a href="http://www.php.net/mysql_close"><span style="color: #000066;">mysql_close</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;</pre>
<p>Now the data has to be requested and handled from the Android side:</p>
<pre class="java">&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #993333;">void</span> initPHP<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #808080; font-style: italic;">//http post</span>
		 <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> result = <span style="color: #ff0000;">&quot;&quot;</span>;
		  <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInputStream+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">InputStream</span></a> is = <span style="color: #000000; font-weight: bold;">null</span> ;
		 <span style="color: #000000; font-weight: bold;">try</span><span style="color: #66cc66;">&#123;</span>
			 DefaultHttpClient httpclient = <span style="color: #000000; font-weight: bold;">new</span> DefaultHttpClient<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		         HttpPost httppost = <span style="color: #000000; font-weight: bold;">new</span> HttpPost<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;http://www.neo-tech.es/defcon/cds.php5&quot;</span><span style="color: #66cc66;">&#41;</span>;
		         HttpResponse response = httpclient.<span style="color: #006600;">execute</span><span style="color: #66cc66;">&#40;</span>httppost<span style="color: #66cc66;">&#41;</span>;
		         HttpEntity entity = response.<span style="color: #006600;">getEntity</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		         is = entity.<span style="color: #006600;">getContent</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		 <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		         Log.<span style="color: #006600;">e</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;log_tag&quot;</span>, <span style="color: #ff0000;">&quot;Error in http connection &quot;</span>+e.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		 <span style="color: #66cc66;">&#125;</span>
		 <span style="color: #808080; font-style: italic;">//convert response to string</span>
		 <span style="color: #000000; font-weight: bold;">try</span><span style="color: #66cc66;">&#123;</span>
		         <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABufferedReader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">BufferedReader</span></a> reader = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ABufferedReader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">BufferedReader</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AInputStreamReader+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">InputStreamReader</span></a><span style="color: #66cc66;">&#40;</span>is,<span style="color: #ff0000;">&quot;iso-8859-1&quot;</span><span style="color: #66cc66;">&#41;</span>,<span style="color: #cc66cc;">8</span><span style="color: #66cc66;">&#41;</span>;
		         StringBuilder sb = <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		         <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AString+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">String</span></a> line = <span style="color: #000000; font-weight: bold;">null</span>;
		         <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>line = reader.<span style="color: #006600;">readLine</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> != <span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		                 sb.<span style="color: #006600;">append</span><span style="color: #66cc66;">&#40;</span>line + <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
		         <span style="color: #66cc66;">&#125;</span>
		         is.<span style="color: #006600;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
		         result=sb.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		 <span style="color: #66cc66;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Exception</span></a> e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		         Log.<span style="color: #006600;">e</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;log_tag&quot;</span>, <span style="color: #ff0000;">&quot;Error converting result &quot;</span>+e.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		 <span style="color: #66cc66;">&#125;</span>
&nbsp;
		 <span style="color: #808080; font-style: italic;">//parse json data</span>
		 <span style="color: #000000; font-weight: bold;">try</span><span style="color: #66cc66;">&#123;</span>
		         JSONArray jArray = <span style="color: #000000; font-weight: bold;">new</span> JSONArray<span style="color: #66cc66;">&#40;</span>result<span style="color: #66cc66;">&#41;</span>;
		         <span style="color: #993333;">int</span> lastDay = <span style="color: #cc66cc;">0</span>;
		         <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #993333;">int</span> i=<span style="color: #cc66cc;">0</span>;i&lt;jArray.<span style="color: #006600;">length</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;i++<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		        	 JSONObject json_data = jArray.<span style="color: #006600;">getJSONObject</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>;
		        	 <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADateFormat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">DateFormat</span></a> df = <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ASimpleDateFormat+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">SimpleDateFormat</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yyyy-MM-dd hh:mm:ss&quot;</span><span style="color: #66cc66;">&#41;</span>;
		        	 <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3ADate+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">Date</span></a> day = <span style="color: #000000; font-weight: bold;">null</span>;
		        	 <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
		        		 day = df.<span style="color: #006600;">parse</span><span style="color: #66cc66;">&#40;</span>json_data.<span style="color: #006600;">getString</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;FechayHora&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		        	 <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3AParseException+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #aaaadd; font-weight: bold;">ParseException</span></a> e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		        		 e.<span style="color: #006600;">printStackTrace</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		        	 <span style="color: #66cc66;">&#125;</span>
		            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>i == <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>
		            	lastDay = day.<span style="color: #006600;">getDay</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		            <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>day.<span style="color: #006600;">getDay</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> != lastDay<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		            	mEntity.<span style="color: #006600;">getCdsData</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">add</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> CDS<span style="color: #66cc66;">&#40;</span>json_data.<span style="color: #006600;">getInt</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Secuencial&quot;</span><span style="color: #66cc66;">&#41;</span>,day,json_data.<span style="color: #006600;">getString</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Pais&quot;</span><span style="color: #66cc66;">&#41;</span>,json_data.<span style="color: #006600;">getDouble</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;CDS&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		            	lastDay = day.<span style="color: #006600;">getDay</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		            <span style="color: #66cc66;">&#125;</span>
		         <span style="color: #66cc66;">&#125;</span>
		 <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>JSONException e<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		         Log.<span style="color: #006600;">e</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;log_tag&quot;</span>, <span style="color: #ff0000;">&quot;Error parsing data &quot;</span>+e.<span style="color: #006600;">toString</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
		 <span style="color: #66cc66;">&#125;</span>
	 <span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>The code can be improved to handle authentication, HTTPS, or do more complex work in general. In a project at work we used authentication based on a pair of generated keys, but a simple method based on a known keyword is enough.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=51</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why Android still sells less than Apple?</title>
		<link>http://www.neo-tech.es/wp/?p=50</link>
		<comments>http://www.neo-tech.es/wp/?p=50#comments</comments>
		<pubDate>Fri, 29 Jul 2011 11:51:52 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Aplicaciones móviles]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[itunes market]]></category>
		<category><![CDATA[market]]></category>

		<guid isPermaLink="false">http://www.neo-tech.es/wp/?p=50</guid>
		<description><![CDATA[As an Android developer, I have been concerned during the last months about why the Android Market seems to be less profitable than the App Store. I have access to some statistics of my application's ports to iPhone/iPad: while the free applications don't seem to be affected, the paid applications have a huge gap with [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center"><img src="http://www.neo-tech.es/wp/images/android-v-iphone.jpg" alt="Android vs iPhone" width="286" height="175" /></p>
<p>As an Android developer, I have been concerned during the last months about why the Android Market seems to be less profitable than the App Store. I have access to some statistics of my application's ports to iPhone/iPad: while the free applications don't seem to be affected, the paid applications have a huge gap with their iOS counterpart: they are downloaded up to 50 times more. I only can feel that I chose the wrong mobile platform to develop for. But why is this happening?</p>
<p>There are a bunch of well known differences between the nature of iOS and Android applications, but not all of them affect the sells. According to the report of <a href="http://www.gartner.com/it/page.jsp?id=1622614" target="_blank">Gartner</a> (April 2011), 38.5% of the smartphone market belongs to Android, while 19.4% belongs to iPhone. It is almost the double for Android, and the growth has been astonishing: from an estimated 3.9% in 2009, it has rapidly overtaken all of their competitors</p>
<p style="text-align: center"><img src="http://www.neo-tech.es/wp/images/androidAppleGraph.jpg" alt="Growing Graph" width="706" height="363" /></p>
<p>But if we look on the <a href="http://report.distimo.com/" target="_blank">statistics</a>, Apple's iPhone App Store is still larger in size than the Android Market. It houses more than 350,000 applications and games, while it was reported in Business Insider in March 2011, that the Android Market has hit the 250,000+ mark. This obviously has as well a dark side: with many competing applications, is harder to sell our own application. Android does not have (yet) as many applications as the iPhone store, but it lacks of a Desktop client (iTunes) and it is much less user friendly (a field where Apple always wins).</p>
<p style="text-align: center"><img src="http://www.neo-tech.es/wp/images/distimo1.png" alt="Number of applications" width="536" height="425" /></p>
<p>So there we go: Android has sold more phones, and iOS has published more applications. One of the first thoughts is that the developers are choosing iOS for a misterious reason. I might point out here one of the main problems with Android: the fragmentation. Whereas Google provides the operative system, the different manufacturers provide the hardware. This leads into a huge uncontrolled mess: different phones may support (or not) different features (i.e., camera, giroscope, etc), which is a problem for developers.</p>
<p>In order to ensure a minimum of compatibility between many devices, Google Android has the <a href="http://source.android.com/compatibility/index.html" target="_blank">Compatibility Program</a>, an automated control mechanism that determines which device is minimally compatible with Android.</p>
<p>The devices that pass the program can use the official logo of Android, and what is truly important, they can integrate the application that gives access to Android Market, Android application repository.</p>
<p>Another reason could be the politics of payment and refund. Payments on the iPhone App Store are handled via your iTunes account which is linked directly to your credit card, whereas Android uses Google Checkout, a paypal clone of Google. Many people have an iTunes account since a few years, but Google Checkout is still relatively unknown (I didn't have an account until I need it to get my revenues from the Android Market!).</p>
<p>Also one major difference between the Android Market and the iPhone App Store is refunds. With Android, you can test an application for 15 minutes and uninstall it in order to obtain a refund. However, this is not allowed in iPhone App Store. This results in greater sales in iPhone Store, enabling more revenue for the developers.</p>
<p>So what exactly is going wrong in the Android Market? In my opinion there are several reasons, first and most important of these is that the Android Market, even though its recent renovation earlier this year, it remains still below the design level of the App Store. And since we download through the device, we really need a good design and features to facilitate the user purchase.</p>
<p style="text-align: center"><img src="http://www.neo-tech.es/wp/images/distimo2.png" alt="Growing prediction" width="524" height="428" /></p>
<p>There are also rumours that Google promotes free and ad-supported applications instead of paid applications, that Android users belong to an economically lower profile than the iOS users, or that they  did not have time to do things better. I want to believe that the main reason is a ponderate mix of all the aforementioned reasons, with a special weight of the last one: Android has been a special challenge for a company that never worked in mobility, and they still need a little bit of time to improve everything. They have already begin to improve the <a href="http://googlemobile.blogspot.com/2011/05/new-ways-to-discover-great-apps-on.html" target="_blank">Android Market</a> and they are doing a <a href="http://www.engadget.com/2010/03/29/exclusive-android-froyo-to-take-a-serious-shot-at-stemming-plat/" target="_blank">huge effort</a> to solve the fragmentation problem. <a href="http://www.amazon.com/mobile-apps/b/ref=sa_menu_mas2?ie=UTF8&amp;node=2350149011" target="_blank">Amazon Store</a> will also be relevant next year.</p>
<p>I think it takes a little but longer, but hopefully Android will eventually get off and will be as profitable for developers as the App Store it is today.</p>
<p>Some other links:</p>
<ul>
<li><a href="http://mobithinking.com/mobile-marketing-tools/latest-mobile-stats" target="_blank">mobiThink</a> report</li>
<li><a href="http://techcrunch.com/2011/04/27/there-are-now-more-free-apps-for-android-than-for-the-ios-platform-distimo/" target="_blank">TechCrunch</a> analysis</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.neo-tech.es/wp/?feed=rss2&#038;p=50</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

