<?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>GaanZa &#187; Developer</title>
	<atom:link href="http://www.gaanza.com/blog/category/developer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gaanza.com</link>
	<description></description>
	<lastBuildDate>Tue, 03 Jan 2012 10:09:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Dude where is my javascript ?</title>
		<link>http://www.gaanza.com/blog/dude-where-is-my-javascript/</link>
		<comments>http://www.gaanza.com/blog/dude-where-is-my-javascript/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 10:33:26 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Developer]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[webview]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=978</guid>
		<description><![CDATA[Hola everyone, a very hectic schedule going on this season. Working my ass off in my upcoming ambitious project. Anyway i am here and writing about just very basic problem that i faced. For a game project i wanted to show leaderboards, for this part i decided to use web view to show leaderboards page [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Hola everyone,</strong><br />
a very hectic schedule going on this season. Working my ass off in my upcoming ambitious project. Anyway i am here and writing about just very basic problem that i faced. For a game project i wanted to show leaderboards, for this part i decided to use web view to show leaderboards page optimized for device. It has some javascript function that is doing its stuff of retrieving leaderboards details and all that. So i wrote a line to load the url pointing to my leaderboard page and guess what it didn&#8217;t rendered fully and i was again left scratching my head <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  . Then after going through many testing i found that stuffs that were dependent on javascript were not getting rendered on my web view so i got the root of the problem i.e dude where is my javascript ? . Then i did a peroid(.) operator on my webview reference variable and found a method setJavaScriptEnabled(), so i did something like this:</p>
<div class="codesnip-container" >
<div class="codesnip">webView.<span class="me1">getSettings</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">setJavaScriptEnabled</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>;</div>
</div>
<p>and yeah u guessed it right, it rendered fully after calling that method. So i guess by default javascript is disabled on webview. Phew.</p>
<p><strong>Gracias.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/dude-where-is-my-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Starting Activity from non-activity class</title>
		<link>http://www.gaanza.com/blog/starting-activity-from-non-activity-class/</link>
		<comments>http://www.gaanza.com/blog/starting-activity-from-non-activity-class/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 08:44:38 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Developer]]></category>
		<category><![CDATA[activity]]></category>
		<category><![CDATA[context]]></category>
		<category><![CDATA[intent]]></category>
		<category><![CDATA[non-activity class]]></category>
		<category><![CDATA[startactivity]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=964</guid>
		<description><![CDATA[Hola everyone, While working on android project i got myself into a situation where i had to start an activity from non-activity class (a class that doesn&#8217;t extend Activity), and i had no other option that time. So lets say there is a class called DummyClass having a dummyMethod() and i had to start an [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Hola everyone,</strong></p>
<p>While working on android project i got myself into a situation where i had to start an activity from non-activity class (a class that doesn&#8217;t extend Activity), and i had no other option that time. So lets say there is a class called DummyClass having a dummyMethod() and i had to start an activity called MeActivity, so i wrote this to start an activity, a rough pseudocode:</p>
<div class="codesnip-container" >
<div class="codesnip">DummyClass<span class="br0">&#123;</span><br />
&#8230;<br />
<span class="me1">dummyMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
Intent dummyIntent = <span class="kw2">new</span> Intent<span class="br0">&#40;</span>DummyClass.<span class="kw2">this</span>, MeActivity.<span class="kw2">class</span><span class="br0">&#41;</span>;<br />
<span class="kw2">this</span>.<span class="me1">startActivity</span><span class="br0">&#40;</span>dummyIntent<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&#8230;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Well as i had my doubts earlier above code is a garbage cuz there is no startActivity() method in my dummyclass. So i did something which worked for me. I declared a public static Context reference variable in MeActivity class and in its constructor i assigned its instance to variable. Here is the code that will look like:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">public</span> <span class="kw2">class</span> MeActivity <span class="kw2">extends</span> Activity<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;<span class="kw2">public</span> <span class="kw4">static</span> <a href="http://www.google.com/search?q=allinurl%3AContext+java.sun.com&#038;bntl=1"><span class="kw3">Context</span></a> meActivityContext;<br />
&nbsp; &nbsp; &nbsp;<span class="kw2">public</span> FavoritesActivity<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; MeActivity.<span class="me1">meActivityContext</span>=<span class="kw2">this</span>;<br />
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>So that was it. Now i used this context variable to start my activity from non-activity class.</p>
<div class="codesnip-container" >
<div class="codesnip">DummyClass<span class="br0">&#123;</span><br />
&#8230;<br />
<span class="me1">dummyMethod</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp;Intent dummyIntent = <span class="kw2">new</span> Intent<span class="br0">&#40;</span>MeActivity.<span class="me1">meActivityContext</span>, MeActivity.<span class="kw2">class</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp;MeActivity.<span class="me1">meActivityContext</span>.<span class="me1">startActivity</span><span class="br0">&#40;</span>dummyIntent<span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
&#8230;<br />
<span class="br0">&#125;</span></div>
</div>
<p>well that worked for me, hope it works for you too.</p>
<p><strong>Gracias.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/starting-activity-from-non-activity-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hide soft keyboard: Android</title>
		<link>http://www.gaanza.com/blog/hide-soft-keyboard-android/</link>
		<comments>http://www.gaanza.com/blog/hide-soft-keyboard-android/#comments</comments>
		<pubDate>Tue, 30 Aug 2011 04:23:47 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Developer]]></category>
		<category><![CDATA[autocompleteview]]></category>
		<category><![CDATA[soft keyboard]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=955</guid>
		<description><![CDATA[I just wanted to share two ways about hiding your soft keyboard. When i was making a screen for submitting score to leaderboards, when i touch edittext to enter my name soft keyboard appears and when i click submit button i wanted that soft keyboard to disappear so i found this one way to do [...]]]></description>
			<content:encoded><![CDATA[<p>I just wanted to share two ways about hiding your soft keyboard. When i was making a screen for submitting score to leaderboards, when i touch edittext to enter my name soft keyboard appears and when i click submit button i wanted that soft keyboard to disappear so i found this one way to do that:</p>
<div class="codesnip-container" >
<div class="codesnip">InputMethodManager inputManager = <span class="br0">&#40;</span>InputMethodManager<span class="br0">&#41;</span>getSystemService<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AContext+java.sun.com&#038;bntl=1"><span class="kw3">Context</span></a>.<span class="me1">INPUT_METHOD_SERVICE</span><span class="br0">&#41;</span>; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; inputManager.<span class="me1">hideSoftInputFromWindow</span><span class="br0">&#40;</span>Test.<span class="kw2">this</span>.<span class="me1">getCurrentFocus</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getWindowToken</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, InputMethodManager.<span class="me1">HIDE_NOT_ALWAYS</span><span class="br0">&#41;</span>;</div>
</div>
<p>Now for second issue that i faced, i had a AutoCompleteTextView on my one application activity so whenever that activity takes screen a soft keyboard appears with focus on autocomplete obviously. This thing didn&#8217;t happened with EditText. But whenever i had autocompletetextview soft keyboard appears automatically. I didn&#8217;t wanted this. Instead i wanted soft keyboard to appear only when i touch AutoCompleteTextView. But i found a simple solution for this:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc3"><span class="re1">&lt;activity</span> android:<span class="re0">name</span>=<span class="st0">&#8220;.MyActivity&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:<span class="re0">windowSoftInputMode</span>=<span class="st0">&#8220;stateAlwaysHidden&#8221;</span><span class="re2">&gt;</span></span></div>
</div>
<p>yeah just add <strong>android:windowSoftInputMode=&#8221;stateAlwaysHidden&#8221;</strong> attribute to activity tag, so whenever my activity takes screen keyboard will not show up until i touch any editable view. This has worked for me till now, so you can give it a try.</p>
<p><strong>Gracias</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/hide-soft-keyboard-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic updating AutoCompleteTextView with remote MySQL data &#8211; Part I</title>
		<link>http://www.gaanza.com/blog/android-autocompletetextview-mysql/</link>
		<comments>http://www.gaanza.com/blog/android-autocompletetextview-mysql/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 05:19:46 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Developer]]></category>
		<category><![CDATA[activity]]></category>
		<category><![CDATA[autocompletetextview]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[how to mysql]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[textwatcher]]></category>
		<category><![CDATA[what is android]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=928</guid>
		<description><![CDATA[Well i have been playing around a lot with android client connectivity with remote MySQL database. Lately i wanted a UI/View, something input textfield, i wanted to show completion suggestions automatically in a drop down menu while the user is typing, suggestions would be coming from MySQL database. Basically whenever i type, i am calling [...]]]></description>
			<content:encoded><![CDATA[<p>Well i have been playing around a lot with android client connectivity with remote MySQL database. Lately i wanted a UI/View, something input textfield, i wanted to show completion suggestions automatically in a drop down menu  while the user is typing, suggestions would be coming from MySQL database. Basically whenever i type, i am calling php sending the input and php will query database and return the values(in my case just names) that have “input” as substring (like if i type ‘pa’ then it would return names that have ‘pa’ as substring). I am sending results to android client via encoded json. In android side i am decoding json and filling my ArrayAdapter. Lets see the points that are being done:</p>
<p><strong>1) PHP</strong></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">&lt;?php</span><br />
<a href="http://www.php.net/mysql_connect"><span class="kw3">mysql_connect</span></a><span class="br0">&#40;</span><span class="st0">&#8220;localhost&#8221;</span>,<span class="st0">&#8220;root&#8221;</span>,<span class="st0">&#8220;&#8221;</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/mysql_select_db"><span class="kw3">mysql_select_db</span></a><span class="br0">&#40;</span><span class="st0">&#8220;dalalstreet&#8221;</span><span class="br0">&#41;</span>;<br />
&nbsp;<br />
<span class="re0">$st</span> = <span class="re0">$_REQUEST</span><span class="br0">&#91;</span><span class="st0">&#8216;st&#8217;</span><span class="br0">&#93;</span>;</p>
<p><span class="re0">$q</span>= <a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&#8220;SELECT * FROM world WHERE company LIKE &#8216;%&#8221;</span>.<span class="re0">$st</span>.<span class="st0">&#8220;%&#8217;&#8221;</span><span class="br0">&#41;</span>;</p>
<p><span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$e</span> = <a href="http://www.php.net/mysql_fetch_assoc"><span class="kw3">mysql_fetch_assoc</span></a><span class="br0">&#40;</span><span class="re0">$q</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$output</span><span class="br0">&#91;</span><span class="br0">&#93;</span>=<span class="re0">$e</span>;<br />
&nbsp;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span>json_encode<span class="br0">&#40;</span><span class="re0">$output</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp;<br />
<a href="http://www.php.net/mysql_close"><span class="kw3">mysql_close</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>I somehow looking at google i wrote this code. This php gets the input text from my android and it runs query at database for records whose company names has “input text” as a substring. You can see the query statement above. In the end it encodes the result in json.</p>
<p><strong>2) Main.xml (my layout)</strong><br />
    Here is the xml layout file for my ui.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc3"><span class="re1">&lt;</span>?xml <span class="re0">version</span>=<span class="st0">&#8220;1.0&#8243;</span> <span class="re0">encoding</span>=<span class="st0">&#8220;utf-8&#8243;</span>?<span class="re2">&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;LinearLayout</span> xmlns:<span class="re0">android</span>=<span class="st0">&#8220;http://schemas.android.com/apk/res/android&#8221;</span><br />
&nbsp; &nbsp; android:<span class="re0">orientation</span>=<span class="st0">&#8220;vertical&#8221;</span><br />
&nbsp; &nbsp; android:layout_<span class="re0">width</span>=<span class="st0">&#8220;fill_parent&#8221;</span><br />
&nbsp; &nbsp; android:layout_<span class="re0">height</span>=<span class="st0">&#8220;fill_parent&#8221;</span><br />
&nbsp; &nbsp; <span class="re2">&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;TextView</span>&nbsp; <br />
&nbsp; &nbsp; android:layout_<span class="re0">width</span>=<span class="st0">&#8220;wrap_content&#8221;</span> <br />
&nbsp; &nbsp; android:layout_<span class="re0">height</span>=<span class="st0">&#8220;wrap_content&#8221;</span> <br />
&nbsp; &nbsp; android:<span class="re0">text</span>=<span class="st0">&#8220;@string/country_label&#8221;</span><br />
&nbsp; &nbsp; <span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;CustomAutoCompleteView</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; android:<span class="re0">id</span>=<span class="st0">&#8220;@+id/autoCompleteCountry&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; android:layout_<span class="re0">width</span>=<span class="st0">&#8220;fill_parent&#8221;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; android:layout_<span class="re0">height</span>=<span class="st0">&#8220;wrap_content&#8221;</span> <span class="re2">/&gt;</span></span><br />
<span class="sc3"><span class="re1">&lt;/LinearLayout<span class="re2">&gt;</span></span></span></div>
</div>
<p><strong>3) Main Activity class</strong><br />
Here is the main class that extends Activity. Here is the code of calling php module and parsing json. I have explained this all in my previous post so you could go through it. i’l explain just AutoCompleteTextView part here.</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="co2">import java.io.BufferedReader;</span><br />
<span class="co2">import java.io.InputStream;</span><br />
<span class="co2">import java.io.InputStreamReader;</span><br />
<span class="co2">import java.util.ArrayList;</span></p>
<p><span class="co2">import org.apache.http.HttpEntity;</span><br />
<span class="co2">import org.apache.http.HttpResponse;</span><br />
<span class="co2">import org.apache.http.NameValuePair;</span><br />
<span class="co2">import org.apache.http.client.HttpClient;</span><br />
<span class="co2">import org.apache.http.client.entity.UrlEncodedFormEntity;</span><br />
<span class="co2">import org.apache.http.client.methods.HttpPost;</span><br />
<span class="co2">import org.apache.http.impl.client.DefaultHttpClient;</span><br />
<span class="co2">import org.apache.http.message.BasicNameValuePair;</span><br />
<span class="co2">import org.json.JSONArray;</span><br />
<span class="co2">import org.json.JSONException;</span><br />
<span class="co2">import org.json.JSONObject;</span></p>
<p><span class="co2">import android.app.Activity;</span><br />
<span class="co2">import android.os.Bundle;</span><br />
<span class="co2">import android.text.Editable;</span><br />
<span class="co2">import android.text.TextWatcher;</span><br />
<span class="co2">import android.util.Log;</span><br />
<span class="co2">import android.widget.ArrayAdapter;</span></p>
<p><span class="kw2">public</span> <span class="kw2">class</span> AutoComptest <span class="kw2">extends</span> Activity <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> CustomAutoCompleteView autoComplete;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> ArrayAdapter&lt;String&gt; autoCompleteAdapter;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/** Called when the activity is first created. */</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> onCreate<span class="br0">&#40;</span>Bundle savedInstanceState<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; super.<span class="me1">onCreate</span><span class="br0">&#40;</span>savedInstanceState<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; setContentView<span class="br0">&#40;</span>R.<span class="me1">layout</span>.<span class="me1">main</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; autoCompleteAdapter = <span class="kw2">new</span> ArrayAdapter&lt;String&gt;<span class="br0">&#40;</span><span class="kw2">this</span>, android.<span class="me1">R</span>.<span class="me1">layout</span>.<span class="me1">simple_dropdown_item_1line</span><span class="br0">&#41;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; autoCompleteAdapter.<span class="me1">setNotifyOnChange</span><span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span>; <span class="co1">// This is so I don&#8217;t have to manually sync whenever changed&nbsp; </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; autoComplete = <span class="br0">&#40;</span>CustomAutoCompleteView<span class="br0">&#41;</span>&nbsp; findViewById<span class="br0">&#40;</span>R.<span class="me1">id</span>.<span class="me1">autoCompleteCountry</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; autoComplete.<span class="me1">setHint</span><span class="br0">&#40;</span><span class="st0">&#8220;Country&#8221;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; autoComplete.<span class="me1">setThreshold</span><span class="br0">&#40;</span><span class="nu0">3</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; autoComplete.<span class="me1">setAdapter</span><span class="br0">&#40;</span>autoCompleteAdapter<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; autoComplete.<span class="me1">addTextChangedListener</span><span class="br0">&#40;</span>textChecker<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw2">final</span> TextWatcher textChecker = <span class="kw2">new</span> TextWatcher<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> afterTextChanged<span class="br0">&#40;</span>Editable s<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> beforeTextChanged<span class="br0">&#40;</span>CharSequence s, <span class="kw4">int</span> start, <span class="kw4">int</span> count, <span class="kw4">int</span> after<span class="br0">&#41;</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><br />
&nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw4">void</span> onTextChanged<span class="br0">&#40;</span>CharSequence s, <span class="kw4">int</span> start, <span class="kw4">int</span> before, <span class="kw4">int</span> count<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; autoCompleteAdapter.<span class="me1">clear</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callPHP<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span>;<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span class="kw2">private</span> <span class="kw4">void</span> callPHP<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a> result = <span class="st0">&#8220;&#8221;</span>;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AInputStream+java.sun.com&#038;bntl=1"><span class="kw3">InputStream</span></a> is=<span class="kw2">null</span>;&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList&lt;NameValuePair&gt; nameValuePairs = <span class="kw2">new</span> ArrayList&lt;NameValuePair&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nameValuePairs.<span class="me1">add</span><span class="br0">&#40;</span><span class="kw2">new</span> BasicNameValuePair<span class="br0">&#40;</span><span class="st0">&#8220;st&#8221;</span>,autoComplete.<span class="me1">getText</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpClient httpclient = <span class="kw2">new</span> DefaultHttpClient<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpPost httppost = <span class="kw2">new</span> HttpPost<span class="br0">&#40;</span><span class="st0">&#8220;http://10.0.2.2/gaanza_android/android_database.php&#8221;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httppost.<span class="me1">setEntity</span><span class="br0">&#40;</span><span class="kw2">new</span> UrlEncodedFormEntity<span class="br0">&#40;</span>nameValuePairs<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpResponse response = httpclient.<span class="me1">execute</span><span class="br0">&#40;</span>httppost<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpEntity entity = response.<span class="me1">getEntity</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;is = entity.<span class="me1">getContent</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="kw2">catch</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&#038;bntl=1"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span class="me1">e</span><span class="br0">&#40;</span><span class="st0">&#8220;log_tag&#8221;</span>, <span class="st0">&#8220;Error in http connection &#8220;</span>+e.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span><span class="br0">&#123;</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?q=allinurl%3ABufferedReader+java.sun.com&#038;bntl=1"><span class="kw3">BufferedReader</span></a> reader = <span class="kw2">new</span> <a href="http://www.google.com/search?q=allinurl%3ABufferedReader+java.sun.com&#038;bntl=1"><span class="kw3">BufferedReader</span></a><span class="br0">&#40;</span><span class="kw2">new</span> <a href="http://www.google.com/search?q=allinurl%3AInputStreamReader+java.sun.com&#038;bntl=1"><span class="kw3">InputStreamReader</span></a><span class="br0">&#40;</span>is,<span class="st0">&#8220;iso-8859-1&#8243;</span><span class="br0">&#41;</span>,<span class="nu0">8</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StringBuilder sb = <span class="kw2">new</span> StringBuilder<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a> line = <span class="kw2">null</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>line = reader.<span class="me1">readLine</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;sb.<span class="me1">append</span><span class="br0">&#40;</span>line + <span class="st0">&#8220;<span class="es0">\n</span>&#8220;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;is.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result=sb.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="kw2">catch</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&#038;bntl=1"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span class="me1">e</span><span class="br0">&#40;</span><span class="st0">&#8220;log_tag&#8221;</span>, <span class="st0">&#8220;Error converting result &#8220;</span>+e.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//parse json data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONArray jArray = <span class="kw2">new</span> JSONArray<span class="br0">&#40;</span>result<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>;i&lt;jArray.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;i++<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject json_data = jArray.<span class="me1">getJSONObject</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span>;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;autoCompleteAdapter.<span class="me1">add</span><span class="br0">&#40;</span>json_data.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&#8220;country&#8221;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">catch</span><span class="br0">&#40;</span>JSONException e<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span class="me1">e</span><span class="br0">&#40;</span><span class="st0">&#8220;log_tag&#8221;</span>, <span class="st0">&#8220;Error parsing data &#8220;</span>+e.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>Note: remember “http://10.0.2.2/&#8221; in my <a href="http://www.gaanza.com/blog/how-to-mysql/" target="_blank">last post</a>. I had to use 10.0.2.2 instead of localhost because emulator consider itself as localhost and my xammp local server is not in emulator localhost.</strong></p>
<p>Ok so i want to detect an event when a text is typed/deleted from my autocompletetextview, basically i want a listener which detects a change in text of my view. So what should we do ? Well in my case i searched the android developer documentation <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  , there i found something called TextWatcher. What i am doing is attach a TextWatcher object to editable like my AutoCompleteTextView so whenever any text changes in my editable the functions/methods of TextWatcher will be called and in these methods i had to write my logic codes which is what i want to achieve if text changes in autocompletetextview ?. There are three abstract methods in TextWatcher class so you have to override them, well anyway thats what we wanted. Methods are:</p>
<blockquote><p>afterTextChanged(Editable s)<br />
	beforeTextChanged(CharSequence c, int start, int count, int after)<br />
	onTextChanged(CharSequence c, int start, int before, int count)</p></blockquote>
<p>Best way to understand them is going through the <a href="http://developer.android.com/reference/android/text/TextWatcher.html" title="TextWatcher" target="_blank">document here</a>. Anyway i had to write my logic code on beforeTextChanged() method so that it performs action whenever there is a change in text of autocompletetextview.</p>
<p>In above code sample i am calling PHP whenever i am typing something. So i am calling callPHP function at onTextChanged() method. So whenever i type something it will call PHP giving text to the function in PHP and PHP will query MySql	database for results and PHP will send results to me in json. This is it. So i decode json and fill the results in ArrayAdapter after clearing it, whenever ArrayAdapter changes it notify the changes being made and AutoCompleteView will show the new drop down suggestions.</p>
<p>There was still one very minor problem and i solved it with my another very stupid solution <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  So problem was that whenever the results from database gets filled in ArrayAdapter and AutoCompleteTextView shows drop down suggestions, it showed me filtered suggestions like suppose i type “can” and i get results(which has “can” as substring) from database, so for “can” ArrayAdapter contents get filtered and i get suggestion starting with text “can” which i didn’t wanted because i already has got filtered results from database and i wanted to show all results that i am receiving from database. So for this i had to write my custom AutoCompleteTextView. It isn’t hard at all, i just had to extend AutoCompleteTextView and override the methods.</p>
<p><strong>4) CustomAutoCompleteView which extends AutoCompletetextView</strong></p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">public</span> <span class="kw2">class</span> CustomAutoCompleteView <span class="kw2">extends</span> AutoCompleteTextView <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> CustomAutoCompleteView<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AContext+java.sun.com&#038;bntl=1"><span class="kw3">Context</span></a> context<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super<span class="br0">&#40;</span>context<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// TODO Auto-generated constructor stub</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> CustomAutoCompleteView<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AContext+java.sun.com&#038;bntl=1"><span class="kw3">Context</span></a> context, <a href="http://www.google.com/search?q=allinurl%3AAttributeSet+java.sun.com&#038;bntl=1"><span class="kw3">AttributeSet</span></a> attrs<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super<span class="br0">&#40;</span>context, attrs<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// TODO Auto-generated constructor stub</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> CustomAutoCompleteView<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AContext+java.sun.com&#038;bntl=1"><span class="kw3">Context</span></a> context, <a href="http://www.google.com/search?q=allinurl%3AAttributeSet+java.sun.com&#038;bntl=1"><span class="kw3">AttributeSet</span></a> attrs,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> defStyle<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super<span class="br0">&#40;</span>context, attrs, defStyle<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// TODO Auto-generated constructor stub</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">protected</span> <span class="kw4">void</span> performFiltering<span class="br0">&#40;</span><span class="kw2">final</span> CharSequence text, <span class="kw2">final</span> <span class="kw4">int</span> keyCode<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a> filterText = <span class="st0">&#8220;&#8221;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.<span class="me1">performFiltering</span><span class="br0">&#40;</span>filterText, keyCode<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; * After a selection, capture the new value and append to the existing<br />
&nbsp; &nbsp; * text<br />
&nbsp; &nbsp; */</span><br />
&nbsp; &nbsp; @Override<br />
&nbsp; &nbsp; <span class="kw2">protected</span> <span class="kw4">void</span> replaceText<span class="br0">&#40;</span><span class="kw2">final</span> CharSequence text<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; super.<span class="me1">replaceText</span><span class="br0">&#40;</span>text<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p><span class="br0">&#125;</span></div>
</div>
<p><strong>How to remove the filter on an ArrayAdapter used in an AutoCompleteTextView?</strong> I had to override performFiltering() method. In this method i am telling AutoCompletetextView to not filter any suggestions, just show all suggestions. I searched on google for this topic but i didn’t find any good solution, maybe i didn’t searched it properly. Whatever, i just wrote a stupid trick to remove any filter, actually instead of technically removing filter i am adding a filter which does a job to show all suggestions <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . I defined a filter which filters all suggestion based on “” yeah a blank string <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . But this works like a charm. Just try it.</p>
<p><strong>So now your are getting results suggestion from database dynamically to AutoCompleteTextView.</strong></p>
<p>But But But But But ok enough, In above code way of fetching data from database to AutoCompletetextView just sucks. Never never ever call a http PHP call in UI Thread. I did a lot of testing with this code. When i was getting small number of results from Database(live database not local) it was working fine but when i was getting large number of results say more than 150 and it was taking a bit of time which caused my UI to be non-responsive and thus famous FORCE CLOSE occured. So never call it from UI thread because u don’t know how much time it will take to complete whole http connection. So always call it in separate thread that i will discuss in Part II of this subject. yes i’l be posting new better version of same problem.</p>
<p><strong>Gracias</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/android-autocompletetextview-mysql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Calling google translate api from flash client</title>
		<link>http://www.gaanza.com/blog/google-translate-flash/</link>
		<comments>http://www.gaanza.com/blog/google-translate-flash/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 10:06:16 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[google translate]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=903</guid>
		<description><![CDATA[Today i was doing some experiment with google translate API. I was trying to use google translate API with AS3. I found many snippets while googling that was using flex but i wanted to do it in flash as3. So I wrote a very basic code snippet to translate text from one language to other [...]]]></description>
			<content:encoded><![CDATA[<p>Today i was doing some experiment with google translate API. I was trying to use google translate API with AS3. I found many snippets while googling that was using flex but i wanted to do it in flash as3. So I wrote a very basic code snippet to translate text from one language to other using google translate API.</p>
<p><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="550" height="400"></p>
<param name="movie" value="http://www.gaanza.com/wp-content/uploads/2011/07/translate.swf" />
<param name="quality" value="high" />
  <embed src="http://www.gaanza.com/wp-content/uploads/2011/07/translate.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="550" height=400"></embed><br />
</object></p>
<p>Well i have created two combobox on my fla. I have given instance name to them and then i am just taking the language input for translating text from one language to other.</p>
<p>The main crucial part:</p>
<blockquote><p>&#8216;http://ajax.googleapis.com/ajax/services/language/translate&#8217;+'?v=2.0&#8242;+&#8217;&#038;q=&#8217;+escape(my_input)+&#8217;&#038;langpair=&#8217;+'es&#8217;+'%7C&#8217;+'en&#8217;</p></blockquote>
<p>I am just making a GET HTTP Call to google translate API passing input, and language pair (i.e en(english) to es(espanol) etc). It returns result in JSON. On my flash client i decode that JSON and display the translated result.<br />
<strong>Note: escape method is used to encode the string to url encoded format</strong></p>
<p>Here is the source code:</p>
<div class="codesnip-container" >
<div class="codesnip">package <br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">import com.adobe.serialization.json.JSON;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">import flash.display.Sprite;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">import flash.events.Event;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">import flash.events.MouseEvent;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">import flash.net.URLLoader;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">import flash.net.URLRequest;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">import flash.net.URLRequestMethod;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">import flash.net.URLVariables;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co2">import fl.data.DataProvider; </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="coMULTI">/**<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * &#8230;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; * @author padam<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; */</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">class</span> Main <span class="kw2">extends</span> Sprite <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> function Main<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw4">void</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>stage<span class="br0">&#41;</span> init<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> addEventListener<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AEvent+java.sun.com&#038;bntl=1"><span class="kw3">Event</span></a>.<span class="me1">ADDED_TO_STAGE</span>, init<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> function init<span class="br0">&#40;</span>e:<a href="http://www.google.com/search?q=allinurl%3AEvent+java.sun.com&#038;bntl=1"><span class="kw3">Event</span></a> = <span class="kw2">null</span><span class="br0">&#41;</span>:<span class="kw4">void</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeEventListener<span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AEvent+java.sun.com&#038;bntl=1"><span class="kw3">Event</span></a>.<span class="me1">ADDED_TO_STAGE</span>, init<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// entry point</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; initGUI<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; button_p.<span class="me1">addEventListener</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AMouseEvent+java.sun.com&#038;bntl=1"><span class="kw3">MouseEvent</span></a>.<span class="me1">CLICK</span>, translate<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> function initGUI<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw4">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var itemsFrom:<a href="http://www.google.com/search?q=allinurl%3AArray+java.sun.com&#038;bntl=1"><span class="kw3">Array</span></a> = <span class="br0">&#91;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>label:<span class="st0">&#8220;English&#8221;</span>, data:<span class="st0">&#8220;en&#8221;</span><span class="br0">&#125;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>label:<span class="st0">&#8220;Spanish&#8221;</span>, data:<span class="st0">&#8220;es&#8221;</span><span class="br0">&#125;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>label:<span class="st0">&#8220;French&#8221;</span>, data:<span class="st0">&#8220;fr&#8221;</span><span class="br0">&#125;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>label:<span class="st0">&#8220;Italian&#8221;</span>, data:<span class="st0">&#8220;it&#8221;</span><span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var itemsTo:<a href="http://www.google.com/search?q=allinurl%3AArray+java.sun.com&#038;bntl=1"><span class="kw3">Array</span></a> = <span class="br0">&#91;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>label:<span class="st0">&#8220;Hindi&#8221;</span>, data:<span class="st0">&#8220;hi&#8221;</span><span class="br0">&#125;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>label:<span class="st0">&#8220;English&#8221;</span>, data:<span class="st0">&#8220;en&#8221;</span><span class="br0">&#125;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>label:<span class="st0">&#8220;Spanish&#8221;</span>, data:<span class="st0">&#8220;es&#8221;</span><span class="br0">&#125;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>label:<span class="st0">&#8220;French&#8221;</span>, data:<span class="st0">&#8220;fr&#8221;</span><span class="br0">&#125;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>label:<span class="st0">&#8220;Italian&#8221;</span>, data:<span class="st0">&#8220;it&#8221;</span> <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fromLanguageCombo.<span class="me1">prompt</span> = <span class="st0">&#8220;Select Language&#8221;</span>; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fromLanguageCombo.<span class="me1">dataProvider</span> = <span class="kw2">new</span> DataProvider<span class="br0">&#40;</span>itemsFrom<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toLanguageCombo.<span class="me1">dataProvider</span> = <span class="kw2">new</span> DataProvider<span class="br0">&#40;</span>itemsTo<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> function translate<span class="br0">&#40;</span>event:<a href="http://www.google.com/search?q=allinurl%3AMouseEvent+java.sun.com&#038;bntl=1"><span class="kw3">MouseEvent</span></a><span class="br0">&#41;</span>:<span class="kw4">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var request:URLRequest=<span class="kw2">new</span> URLRequest<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">url</span>=<span class="st0">&#8216;http://ajax.googleapis.com/ajax/services/language/translate&#8217;</span>+<span class="st0">&#8216;?v=2.0&#8242;</span>+<span class="st0">&#8216;&amp;q=&#8217;</span>+escape<span class="br0">&#40;</span>fromInput.<span class="me1">text</span><span class="br0">&#41;</span>+<span class="st0">&#8216;&amp;langpair=&#8217;</span>+fromLanguageCombo.<span class="me1">selectedItem</span>.<span class="me1">data</span>+<span class="st0">&#8216;%7C&#8217;</span>+toLanguageCombo.<span class="me1">selectedItem</span>.<span class="me1">data</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.<span class="me1">method</span> = URLRequestMethod.<span class="me1">GET</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var loader:URLLoader=<span class="kw2">new</span> URLLoader<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">addEventListener</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AEvent+java.sun.com&#038;bntl=1"><span class="kw3">Event</span></a>.<span class="me1">COMPLETE</span>,messageSent<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; loader.<span class="me1">load</span><span class="br0">&#40;</span>request<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">catch</span> <span class="br0">&#40;</span>error:<a href="http://www.google.com/search?q=allinurl%3AError+java.sun.com&#038;bntl=1"><span class="kw3">Error</span></a><span class="br0">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> function messageSent<span class="br0">&#40;</span>evt:<a href="http://www.google.com/search?q=allinurl%3AEvent+java.sun.com&#038;bntl=1"><span class="kw3">Event</span></a><span class="br0">&#41;</span>:<span class="kw4">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var loader:URLLoader=URLLoader<span class="br0">&#40;</span>evt.<span class="me1">target</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result:<a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a> = loader.<span class="me1">data</span> as <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var json_<span class="nu0">1</span>:<a href="http://www.google.com/search?q=allinurl%3AObject+java.sun.com&#038;bntl=1"><span class="kw3">Object</span></a> = JSON.<span class="me1">decode</span><span class="br0">&#40;</span>result<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.<span class="me1">text</span> = json_<span class="nu0">1</span><span class="br0">&#91;</span><span class="st0">&#8220;responseData&#8221;</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="st0">&#8220;translatedText&#8221;</span><span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; translationLabel.<span class="me1">text</span> = fromLanguageCombo.<span class="me1">selectedItem</span>.<span class="me1">label</span> + <span class="st0">&#8221; to &#8220;</span> + toLanguageCombo.<span class="me1">selectedItem</span>.<span class="me1">label</span>+<span class="st0">&#8221; translation&#8221;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span class="br0">&#125;</span></div>
</div>
<p>Well there is nothing much in code. Just taking the language values from two combo box list and passing it to the translate api. Then after decoding json that i received from api call, i am displaying on textfield. Anyway please note that translate api has been officialy depracated.</p>
<p>Here is the quote from <a href="http://code.google.com/apis/language/translate/overview.html">API site</a>:</p>
<blockquote><p>Important: The Google Translate API has been officially deprecated as of May 26, 2011. Due to the substantial economic burden caused by extensive abuse, the number of requests you may make per day will be limited and the API will be shut off completely on December 1, 2011. For website translations, we encourage you to use the Google Translate Element.</p></blockquote>
<p><strong>Gracias.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/google-translate-flash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Connecting Android to remote mysql via PHP and JSON</title>
		<link>http://www.gaanza.com/blog/how-to-mysql/</link>
		<comments>http://www.gaanza.com/blog/how-to-mysql/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 05:22:12 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Developer]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=881</guid>
		<description><![CDATA[While working on submitting and retrieving global game high scores from a remote database like mysql i learned few things. During start experiment i decided to use PHP to connect to mysql database. What i was trying to do is that i wanted to call php, then php would make a database connection and make [...]]]></description>
			<content:encoded><![CDATA[<p>While working on submitting and retrieving global game high scores from a remote database like mysql i learned few things. During start experiment i decided to use PHP to connect to mysql database. What i was trying to do is that i wanted to call php, then php would make a database connection and make a sql query and retrieve the result, then php would encode that result into JSON format and i would decode this JSON at my android program and display the results. I haven&#8217;t done much PHP before(never actually setup PHP server before <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> ). So i came to know this xampp server, so i installed/setup the xampp server(its a php apache server and it comes with mysql also). I am not telling how to setup xampp and create a databse and tables as you can find it in their wiki.</p>
<p>I create a php file, which makes a database connection and makes a query and encodes a result. As i told before i am not familiar to PHP so i wrote this code looking here and there by googling it <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . Code looks like this:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">&lt;?php</span><br />
<a href="http://www.php.net/mysql_connect"><span class="kw3">mysql_connect</span></a><span class="br0">&#40;</span><span class="st0">&#8220;host&#8221;</span>,<span class="st0">&#8220;username&#8221;</span>,<span class="st0">&#8220;password&#8221;</span><span class="br0">&#41;</span>;<br />
<a href="http://www.php.net/mysql_select_db"><span class="kw3">mysql_select_db</span></a><span class="br0">&#40;</span><span class="st0">&#8220;yourdatabsename&#8221;</span><span class="br0">&#41;</span>;<br />
&nbsp;<br />
<span class="re0">$q</span>=<a href="http://www.php.net/mysql_query"><span class="kw3">mysql_query</span></a><span class="br0">&#40;</span><span class="st0">&#8220;SELECT * FROM yourtable&#8221;</span><span class="br0">&#41;</span>;<br />
<span class="kw1">while</span><span class="br0">&#40;</span><span class="re0">$e</span>=<a href="http://www.php.net/mysql_fetch_assoc"><span class="kw3">mysql_fetch_assoc</span></a><span class="br0">&#40;</span><span class="re0">$q</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$output</span><span class="br0">&#91;</span><span class="br0">&#93;</span>=<span class="re0">$e</span>;<br />
&nbsp;<br />
<a href="http://www.php.net/print"><span class="kw3">print</span></a><span class="br0">&#40;</span>json_encode<span class="br0">&#40;</span><span class="re0">$output</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp;<br />
<a href="http://www.php.net/mysql_close"><span class="kw3">mysql_close</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="kw2">?&gt;</span></div>
</div>
<p>Well code is not that hard to understand. You are making a mysql connection by providing credentials. Then you select a databse. Then you execute a query and result is assigned to $q. Next what i seen is that result is in ArrayCollection type (i saw it in xampp console) so i think we are converting it into Array while iterating through collection(I am not quite sure so if u know then please share). Then You are just encoding the result into JSON.</p>
<p>Now lets see how we will decode the JSON at our android program. Code looks like this:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw4">void</span> parseJSON<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a> result = <span class="st0">&#8220;&#8221;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a> x = <span class="st0">&#8220;&#8221;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=allinurl%3AInputStream+java.sun.com&#038;bntl=1"><span class="kw3">InputStream</span></a> is=<span class="kw2">null</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//http post</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ArrayList&lt;NameValuePair&gt; nameValuePairs = <span class="kw2">new</span> ArrayList&lt;NameValuePair&gt;<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpClient httpclient = <span class="kw2">new</span> DefaultHttpClient<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpPost httppost = <span class="kw2">new</span> HttpPost<span class="br0">&#40;</span><span class="st0">&#8220;http://localhost/game_php_testing/android_database.php&#8221;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;httppost.<span class="me1">setEntity</span><span class="br0">&#40;</span><span class="kw2">new</span> UrlEncodedFormEntity<span class="br0">&#40;</span>nameValuePairs<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpResponse response = httpclient.<span class="me1">execute</span><span class="br0">&#40;</span>httppost<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpEntity entity = response.<span class="me1">getEntity</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;is = entity.<span class="me1">getContent</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="kw2">catch</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&#038;bntl=1"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span class="me1">e</span><span class="br0">&#40;</span><span class="st0">&#8220;log_tag&#8221;</span>, <span class="st0">&#8220;Error in http connection &#8220;</span>+e.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//convert response to string</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?q=allinurl%3ABufferedReader+java.sun.com&#038;bntl=1"><span class="kw3">BufferedReader</span></a> reader = <span class="kw2">new</span> <a href="http://www.google.com/search?q=allinurl%3ABufferedReader+java.sun.com&#038;bntl=1"><span class="kw3">BufferedReader</span></a><span class="br0">&#40;</span><span class="kw2">new</span> <a href="http://www.google.com/search?q=allinurl%3AInputStreamReader+java.sun.com&#038;bntl=1"><span class="kw3">InputStreamReader</span></a><span class="br0">&#40;</span>is,<span class="st0">&#8220;iso-8859-1&#8243;</span><span class="br0">&#41;</span>,<span class="nu0">8</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StringBuilder sb = <span class="kw2">new</span> StringBuilder<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<a href="http://www.google.com/search?q=allinurl%3AString+java.sun.com&#038;bntl=1"><span class="kw3">String</span></a> line = <span class="kw2">null</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">while</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>line = reader.<span class="me1">readLine</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;sb.<span class="me1">append</span><span class="br0">&#40;</span>line + <span class="st0">&#8220;<span class="es0">\n</span>&#8220;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;is.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;result=sb.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="kw2">catch</span><span class="br0">&#40;</span><a href="http://www.google.com/search?q=allinurl%3AException+java.sun.com&#038;bntl=1"><span class="kw3">Exception</span></a> e<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span class="me1">e</span><span class="br0">&#40;</span><span class="st0">&#8220;log_tag&#8221;</span>, <span class="st0">&#8220;Error converting result &#8220;</span>+e.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//parse json data</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">try</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONArray jArray = <span class="kw2">new</span> JSONArray<span class="br0">&#40;</span>result<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="kw1">for</span><span class="br0">&#40;</span><span class="kw4">int</span> i=<span class="nu0">0</span>;i&lt;jArray.<span class="me1">length</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;i++<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;JSONObject json_data = jArray.<span class="me1">getJSONObject</span><span class="br0">&#40;</span>i<span class="br0">&#41;</span>;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span class="me1">i</span><span class="br0">&#40;</span><span class="st0">&#8220;log_tag&#8221;</span>,<span class="st0">&#8220;id: &#8220;</span>+json_data.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&#8220;id&#8221;</span><span class="br0">&#41;</span>+<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#8220;, name: &#8220;</span>+json_data.<span class="me1">getString</span><span class="br0">&#40;</span><span class="st0">&#8220;Player_name&#8221;</span><span class="br0">&#41;</span>+<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="st0">&#8220;, price: &#8220;</span>+json_data.<span class="me1">getDouble</span><span class="br0">&#40;</span><span class="st0">&#8220;score&#8221;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">catch</span><span class="br0">&#40;</span>JSONException e<span class="br0">&#41;</span><span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Log.<span class="me1">e</span><span class="br0">&#40;</span><span class="st0">&#8220;log_tag&#8221;</span>, <span class="st0">&#8220;Error parsing data &#8220;</span>+e.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span></div>
</div>
<p>I faced two problems/exception while connecting to php. PHP was connecting to mysql perfectly as when i run in browser it printed results so there was some issues on my above code which i&#8217;l address here and this is common problems which will be faced by every newcomer to android like me <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . Before that i&#8217;l just explain my php file and localhost path setup. When i installed xampp, in xampp folder there must be folder called htDocs so i have to make a project folder here inside htDocs and inside this project folder will be my php. So my php path would look like this &#8220;/htDocs/game_php_testing/android_database.php&#8221; So when i will make a call to php via &#8220;http://localhost/game_php_testing/android_database.php&#8221;</p>
<p>Now i will address the two problems that i faced.<br />
First, </p>
<blockquote><p>java.net.socketexception -permission denied</p></blockquote>
<p>yes whenever i was trying to call my php i was getting this exception. How did i solved it ??? Well i just provided a permission to internet access at AndroidManifest.xml file. You have to put this permission above <application> tag</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc3"><span class="re1">&lt;uses</span>-permission android:<span class="re0">name</span>=<span class="st0">&#8220;android.permission.INTERNET&#8221;</span> <span class="re2">/&gt;</span></span></div>
</div>
<p>Now second Problem, this one is very important.</p>
<blockquote><p>java.net.ConnectException -Connection Refused.</p></blockquote>
<p>Well what i found is that when u call localhost android thinks that u r calling to android emulator itself not the localhost machine. So localhost/127.0.0.1 wouldn&#8217;t work. So i found the solution to that: instead of localhost use <strong>10.0.2.2</strong>, code will be like this:</p>
<div class="codesnip-container" >
<div class="codesnip">HttpPost httppost = <span class="kw2">new</span> HttpPost<span class="br0">&#40;</span><span class="st0">&#8220;http://10.0.2.2/game_php_testing/android_database.php&#8221;</span><span class="br0">&#41;</span>;</div>
</div>
<p>Well problem solved <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . Now i decode the json that i got from php. I am printing the result on LOG. Please see that when i am calling &#8220;<strong>json_data.getString(&#8220;Player_name&#8221;)</strong>&#8220;, Player_name is a column/attribute in my database table and its type is varchar so i used getString() on it.</p>
<p>I hope this post helped you as you may also faced similar problem at first time.</p>
<p>Gracias.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/how-to-mysql/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Eratosthenes Sieve: Finding Prime Numbers</title>
		<link>http://www.gaanza.com/blog/prime-number/</link>
		<comments>http://www.gaanza.com/blog/prime-number/#comments</comments>
		<pubDate>Thu, 12 May 2011 06:18:38 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[prime]]></category>
		<category><![CDATA[prime number]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sieve of eratosthenes]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=824</guid>
		<description><![CDATA[Yesterday few of my juniors from school were asking me for some good fast prime number finder algorithm but simple. They used to find prime numbers by testing divisibility by all integers from 2 to number-1. This is very basic routine, i remember i also used to use it when i first started. So anyway [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday few of my juniors from school were asking me for some good fast prime number finder algorithm but simple. They used to find prime numbers by testing divisibility by all integers from 2 to number-1. This is very basic routine, i remember i also used to use it when i first started. So anyway they wanted to know some fast, efficient and also simple routine to find prime number. So i told them about <strong>&#8220;<a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Eratosthenes Sieve algorithm</a>&#8220;</strong>, its fast, efficient and simple also. So i quickly wrote a module in python and gave them <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw1">def</span> findPrimeNumbers<span class="br0">&#40;</span><span class="kw2">max</span><span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; nlist=<span class="kw2">range</span><span class="br0">&#40;</span><span class="nu0">2</span>,<span class="kw2">max</span>+<span class="nu0">1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; maxM=<span class="kw3">math</span>.<span class="me1">ceil</span><span class="br0">&#40;</span><span class="kw3">Math</span>.<span class="me1">sqrt</span><span class="br0">&#40;</span><span class="kw2">max</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; index=<span class="nu0">0</span><br />
&nbsp; &nbsp; <span class="kw1">while</span> nlist<span class="br0">&#91;</span>index<span class="br0">&#93;</span> &lt; maxM:<br />
&nbsp; &nbsp; &nbsp; &nbsp; prime=nlist<span class="br0">&#91;</span>index<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; index2=index+<span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> index2 &lt; <span class="kw2">len</span><span class="br0">&#40;</span>nlist<span class="br0">&#41;</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> nlist<span class="br0">&#91;</span>index2<span class="br0">&#93;</span> % prime==<span class="nu0">0</span>:<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nlist.<span class="me1">pop</span><span class="br0">&#40;</span>index2<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index2+=<span class="nu0">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; index+=<span class="nu0">1</span><br />
&nbsp; &nbsp; <span class="kw1">print</span> nlist</div>
</div>
<p><strong>Gracias</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/prime-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DestroyBody de BOX2D Flash</title>
		<link>http://www.gaanza.com/blog/destroybody-box2d/</link>
		<comments>http://www.gaanza.com/blog/destroybody-box2d/#comments</comments>
		<pubDate>Mon, 09 May 2011 05:08:25 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Box 2d]]></category>
		<category><![CDATA[Developer]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[destroy body]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=802</guid>
		<description><![CDATA[Destroying box2d(Box2D Flash) objects is always not smooth, if you don’t manage the references properly. Well sometimes it doesn’t get destroyed also even after calling DestroyBody() method because there is still some reference to box2d objects that is active and u have to keep track of that. I had this problem more often. But yeah [...]]]></description>
			<content:encoded><![CDATA[<p>Destroying box2d(Box2D Flash) objects  is always not smooth, if you don’t manage the references properly. Well sometimes it doesn’t get destroyed also even after calling DestroyBody() method because there is still some reference to box2d objects that is active and u have to keep track of that. I had this problem more often. But yeah i again came up with my just stupid solution to tackle above problem and it works. I have tested this in my all Box2D Flash projects.</p>
<p>What i generally do is create a custom class which would represent particular box2d body, for example in pool game i would create a class called Ball. So now i would create a method called <strong>getDestructionStatus()</strong> which tells me if particular body is eligible for destruction. And second method i would define called <strong>destroy()</strong>, in destroy() method you can write your cleanup() implementation like if u have some movieclip associated then you can remove it from stage <strong>[Note: I didn't call DestroyBody in destroy() because it didn't work for me]</strong>  Something like this :</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">public</span> function destroy<span class="br0">&#40;</span><span class="br0">&#41;</span>:<span class="kw4">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//your custom cleanup code before making box2d body eligible for destruction</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; destructionStatus = <span class="kw2">true</span>;&nbsp; &nbsp; &nbsp; &nbsp;<span class="co1">//eligible for destruction&nbsp; &nbsp; &nbsp;</span><br />
<span class="br0">&#125;</span></div>
</div>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">public</span> function getDestructionStatus<span class="br0">&#40;</span><span class="br0">&#41;</span>:<a href="http://www.google.com/search?q=allinurl%3ABoolean+java.sun.com&#038;bntl=1"><span class="kw3">Boolean</span></a> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">return</span> destructionStatus;<br />
<span class="br0">&#125;</span></div>
</div>
<p>Now how would u implement ? Well lets say if my ball collides with wall then i want to destroy the ball, so code would look like this:</p>
<div class="codesnip-container" >
<div class="codesnip">override <span class="kw2">public</span> function BeginContact<span class="br0">&#40;</span>contact:b2Contact<span class="br0">&#41;</span>:<span class="kw4">void</span> <br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="br0">&#40;</span>contact.<span class="me1">GetFixtureA</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">GetBody</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">GetUserData</span><span class="br0">&#40;</span><span class="br0">&#41;</span> is Wall &amp;&amp; contact.<span class="me1">GetFixtureB</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">GetBody</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">GetUserData</span><span class="br0">&#40;</span><span class="br0">&#41;</span> is Ball<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contact.<span class="me1">GetFixtureB</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">GetBody</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">SetAwake</span><span class="br0">&#40;</span><span class="kw2">false</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contact.<span class="me1">GetFixtureB</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">GetBody</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">GetUserData</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">destroy</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Above i am using a custom class that extends b2ContactListener class and i am overriding BeginContact to determine the collision and provide my some custom implementation response. Here is a insight on b2ContactListener tutorial from <a href="http://blog.allanbishop.com/box2d-2-1a-tutorial-%E2%80%93-part-4-collision-detection/">Allan Bishop</a>.</p>
<p>Now when my ball collides with wall then i call destroy method on it, which runs my custom cleanup and set the destructionState to true. Now i will check for this destructionState and call DestroyMethod, calling DestroyBody in correct context and block matters because as i said if some reference is still pointing to body then it won&#8217;t get detroyed. Here is the implementation code:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw1">for</span> <span class="br0">&#40;</span>var worldBody:b2Body = world.<span class="me1">GetBodyList</span><span class="br0">&#40;</span><span class="br0">&#41;</span>; worldBody; worldBody = worldBody.<span class="me1">GetNext</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>worldBody.<span class="me1">GetUserData</span><span class="br0">&#40;</span><span class="br0">&#41;</span> != <span class="kw2">null</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>worldBody.<span class="me1">GetUserData</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">getDestructionStatus</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; world.<span class="me1">DestroyBody</span><span class="br0">&#40;</span>worldBody<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//write a code to update the position, rotation whatever you want of bodies</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>Now comes the main part:<br />
<strong>I want to explicitly point out that i have put DestroyBody call inside the world body list iteration</strong> because it always worked for me without any issues, my bodies got destroyed and that was what i wanted. I hope it works for you also. I also want to try other techniques, so if you apply other way to destroy objects then please share with us. It would help me and others a lot xD</p>
<p><strong>Gracias</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/destroybody-box2d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LoaderMax befriends Firefox</title>
		<link>http://www.gaanza.com/blog/loadermax/</link>
		<comments>http://www.gaanza.com/blog/loadermax/#comments</comments>
		<pubDate>Mon, 02 May 2011 06:31:07 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[loadermax]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=770</guid>
		<description><![CDATA[For my portfolio flash site i am using LoaderMax from greensock. Why did i use LoaderMax ? Well first i just started using LoaderMax and i am new to it. I am using LoaderMax for two very basic reason: 1) Loading multiple images easily. Well i want to calculate whole progress of multiple assets loading [...]]]></description>
			<content:encoded><![CDATA[<p>For my portfolio flash site i am using LoaderMax from greensock. Why did i use LoaderMax ? Well first i just started using LoaderMax and i am new to it. I am using LoaderMax for two very basic reason:<br />
1) <strong> Loading multiple images easily.</strong> Well i want to calculate whole progress of multiple assets loading and show it in loading progress which was difficult for me to do when doing it in traditional sequential manner (using loader class). It was tough to find bytes total of all images as i used to load images one by one in sequence.<br />
2)  <strong>Garbage collection/resource release:</strong> When i used to load images using typical loader class, i used to face a problem though i later solved it but still i don’t have to go extra step in case of LoaderMax. The problem was when loading was in progress and i closed my browser window then an error was thrown saying “loading never complete” as shown in my previous post. But with LoaderMax i don’t have to worry about it because it releases the memory if loading process gets paused or aborted.</p>
<p>But in this post i am not discussing the tutorial to use LoaderMax or its features. As always i faced a certain problem and i solved it xD. So i wrote a code module including LoaderMax which ran flawlessly on all web browsers (internet explorer, chrome, opera, and safari) but it didn’t run on Mozilla Firefox. <strong>When running on Firefox an exception error was getting thrown</strong> and i spent 1 hour to find the reason and couldn’t find it and got me pissed me off xD. First let me show you the code module that i wrote and was throwing exception on firefox:</p>
<div class="codesnip-container" >
<div class="codesnip">var queue:LoaderMax = <span class="kw2">new</span> LoaderMax<span class="br0">&#40;</span><span class="br0">&#123;</span>name:<span class="st0">&#8220;mainQueue&#8221;</span>, onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler<span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</div>
<div class="codesnip-container" >
<div class="codesnip">queue.<span class="me1">append</span><span class="br0">&#40;</span> <span class="kw2">new</span> ImageLoader<span class="br0">&#40;</span><span class="st0">&#8220;img/photo1.jpg&#8221;</span>, <span class="br0">&#123;</span>name:<span class="st0">&#8220;photo1&#8243;</span><span class="br0">&#125;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</div>
<div class="codesnip-container" >
<div class="codesnip"><span class="co1">//start loading</span><br />
queue.<span class="me1">load</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</div>
<div class="codesnip-container" >
<div class="codesnip">function completeHandler<span class="br0">&#40;</span>event:LoaderEvent<span class="br0">&#41;</span>:<span class="kw4">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; var image:ContentDisplay = LoaderMax.<span class="me1">getContent</span><span class="br0">&#40;</span><span class="st0">&#8220;photo1&#8243;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p>here is the screenshot of error:<br />
<a href="http://www.gaanza.com/wp-content/uploads/2011/05/error_loadermax.png"><img src="http://www.gaanza.com/wp-content/uploads/2011/05/error_loadermax.png" alt="" title="error_loadermax" width="776" height="278" class="aligncenter size-full wp-image-784" /></a></p>
<p>In first statement of function completeHandler i am retrieving loaded image. I am sending it to my resize algorithm later to resize the original image to fit into my target area. But it throws error on firefox because it says invalid bitmapdata. My resize algorithm takes displayobject as parameter and ContentDisplay extends Sprite class. <strong>Note: LoaderMax can also resize your original image according to aspect ratio</strong></p>
<p>I didn&#8217;t understand why this error was coming in firefox but not in other browsers. At first i thought maybe my algorithm was having issues with type ContentDisplay so i re-wrote my completeHandler function as follows:</p>
<div class="codesnip-container" >
<div class="codesnip">function completeHandler<span class="br0">&#40;</span>event:LoaderEvent<span class="br0">&#41;</span>:<span class="kw4">void</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; var image:Bitmap = LoaderMax.<span class="me1">getLoader</span><span class="br0">&#40;</span><span class="st0">&#8220;photo1&#8243;</span><span class="br0">&#41;</span>.<span class="me1">rawContent</span>;<br />
<span class="br0">&#125;</span></div>
</div>
<p><strong>The above new statement gives me bitmap.</strong> Now i run my module and guess what ? yay yepee it didn&#8217;t run on firefox but ran on other browsers xD and this time i got null exception, yeah above statement returned null, weird rite ?? I was frustrated and cussing firefox xD. But accidently while doing just trial and error i added few parameters and viola boom bang dang it got running on firefox and all browsers xD. here is the new change that i made :</p>
<div class="codesnip-container" >
<div class="codesnip">queue.<span class="me1">append</span><span class="br0">&#40;</span> <span class="kw2">new</span> ImageLoader<span class="br0">&#40;</span><span class="st0">&#8220;img/photo1.jpg&#8221;</span>, <span class="br0">&#123;</span>name:<span class="st0">&#8220;photo1&#8243;</span>, estimatedBytes:<span class="nu0">2400</span><span class="br0">&#125;</span><span class="br0">&#41;</span> <span class="br0">&#41;</span>;</div>
</div>
<p>well if u have noticed, i have added new parameter called <strong>&#8220;estimatedBytes&#8221;</strong>. Yes, just this parameter solved above problem and again i was left scratching my head heheheh xD.</p>
<p><strong>Gracias.</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/loadermax/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>accessing a movieclip from moviematerial on Papervision3d Plane</title>
		<link>http://www.gaanza.com/blog/movieclip-papervision/</link>
		<comments>http://www.gaanza.com/blog/movieclip-papervision/#comments</comments>
		<pubDate>Sat, 16 Apr 2011 09:34:35 +0000</pubDate>
		<dc:creator>padam</dc:creator>
				<category><![CDATA[Developer]]></category>
		<category><![CDATA[Flash]]></category>
		<category><![CDATA[Papervision]]></category>
		<category><![CDATA[3d flash]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[Movie Clip]]></category>

		<guid isPermaLink="false">http://www.gaanza.com/?p=757</guid>
		<description><![CDATA[Yeah again i faced a little strange problem that i solved it. In my one of previous post i wrote about using movieclip with plane primitives of papervision and also accessing that movie. Now comes the main discussion of this post that is accessing movieclip from papervision plane. Lets say i have attached a mouse [...]]]></description>
			<content:encoded><![CDATA[<p>Yeah again i faced a little strange problem that i solved it. In my one of previous post i wrote about using movieclip with plane primitives of papervision and also accessing that movie. Now comes the main discussion of this post that is <strong>accessing movieclip from papervision plane.</strong></p>
<p>Lets say i have attached a mouse listener to plane to which moviematerial is mapped. So when i click plane i want to do/manipulate the movieclip that is mapped to plane via moviematerial so code would look something like this:</p>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">private</span> function planePressed<span class="br0">&#40;</span>event:InteractiveScene3DEvent<span class="br0">&#41;</span>:<span class="kw4">void</span><span class="br0">&#123;</span><br />
var moviemc=event.<span class="me1">target</span>.<span class="me1">material</span>.<span class="me1">movie</span> as Movieclip;<br />
<span class="co1">// do manipulation or changes on moviemc</span><br />
<span class="br0">&#125;</span></div>
</div>
<p>the above snippet works, i mean u can access the movieclip on above do the later changes to it if u want.</p>
<p>But lets say there is no listener attached to the plane but later in program cycle i want to modify the movieclip attached to plane like applying blurr filter to it. Then i would do something like this:</p>
<div class="codesnip-container" >
<div class="codesnip">var mc=my_papervision_plane.<span class="me1">material</span>.<span class="me1">movie</span> as Movieclip;<br />
<span class="co1">//apply blurr filter to mc </span><br />
&nbsp;</div>
</div>
<p>Well the above code didn&#8217;t work for me. In previous above event handler i traced event.target and it traced plane object, so it seems like &#8216;.material&#8217; returned MaterialObject3D so i have to first downcast it to MovieMaterial separately and then try to access movie property on it. Here is the error:<br />
<a href="http://www.gaanza.com/wp-content/uploads/2011/04/movie_error.png"><img class="aligncenter size-full wp-image-758" title="movie_error" src="http://www.gaanza.com/wp-content/uploads/2011/04/movie_error.png" alt="" width="797" height="66" /></a></p>
<p>So here is how is the stupid solution <img src='http://www.gaanza.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<div class="codesnip-container" >
<div class="codesnip">var mat=my_papervision_plane.<span class="me1">material</span> as MovieMaterial;<br />
<span class="kw1">if</span><span class="br0">&#40;</span>mat<span class="br0">&#41;</span><span class="br0">&#123;</span><span class="co1">//just being safe</span><br />
var mc=mat.<span class="me1">movie</span> as Movieclip;<br />
<span class="br0">&#125;</span></div>
</div>
<p>So that worked. But still i didn&#8217;t had to do that inside my event handler when i was accessing via even.target, maybe i am missing something out. Right now i am going through docs of papervision.</p>
<p><strong>Gracias</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.gaanza.com/blog/movieclip-papervision/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

