""

Tag Archive: Android

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’t done much PHP before(never actually setup PHP server before :D). 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.

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 :D. Code looks like this:


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.

Now lets see how we will decode the JSON at our android program. Code looks like this:

void parseJSON()
    {
    	
    	String result = "";
    	String x = "";
    	InputStream is=null;
    	//http post
    	try{
    			ArrayList nameValuePairs = new ArrayList();
    	        HttpClient httpclient = new DefaultHttpClient();
    	        HttpPost httppost = new HttpPost("http://localhost/game_php_testing/android_database.php");
    	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    	        HttpResponse response = httpclient.execute(httppost);
    	        HttpEntity entity = response.getEntity();
    	        is = entity.getContent();
    	}catch(Exception e){
    	        Log.e("log_tag", "Error in http connection "+e.toString());
    	}
    	//convert response to string
    	try{
    		 	
    	        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
    	        StringBuilder sb = new StringBuilder();
    	        String line = null;
    	        while ((line = reader.readLine()) != null) {
    	                sb.append(line + "\n");
    	        }
    	        is.close();
    	 
    	        result=sb.toString();
    	        
    	}catch(Exception e){
    	        Log.e("log_tag", "Error converting result "+e.toString());
    	}
    	 

//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i catch(JSONException e){ Log.e(“log_tag”, “Error parsing data “+e.toString()); } }

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’l address here and this is common problems which will be faced by every newcomer to android like me :D. Before that i’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 “/htDocs/game_php_testing/android_database.php” So when i will make a call to php via “http://localhost/game_php_testing/android_database.php”

Now i will address the two problems that i faced.
First,

java.net.socketexception -permission denied

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 tag


Now second Problem, this one is very important.

java.net.ConnectException -Connection Refused.

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’t work. So i found the solution to that: instead of localhost use 10.0.2.2, code will be like this:

HttpPost httppost = new HttpPost("http://10.0.2.2/game_php_testing/android_database.php");

Well problem solved :D. Now i decode the json that i got from php. I am printing the result on LOG. Please see that when i am calling “json_data.getString(“Player_name”)“, Player_name is a column/attribute in my database table and its type is varchar so i used getString() on it.

I hope this post helped you as you may also faced similar problem at first time.

Gracias.

Chrome to phone

Well i have been using chrome to phone extension on my android 2.2.2, and its so magic and useful in my daily life. Anyway Chrome to Phone app is available on android market. But in some countries it is not available. In my country it wasn’t available in market so i found the apk file of google chrome to phone in some website forum that i don’t remember. So if u want to download and install this app on your android then scan this link:google chrome to phone

The App version is 2.2.0

I installed the extension plugin for my chrome browser from chrome web store. Here is the video on installing and using and what its all about:

Gracias.

Adobe Flash on Android ?


I have seen demos showing flash sites opening on high end android devices like HTC Desire HD or Nexus One. So i was looking through the posts about installing adobe flash player 10.1 on my lg optimus one p500 (android 2.2). The author of that particular blog explained a few steps to install flash 10.1 on android 2.2 phones (specifically mentioning my p500),

1) First The author provided a link to adobe flash player 10.1 as it is not available in android market right now(flash player 10.3 is available). So I downloaded it and installed it on my lg optimus p500. It got installed without having any trouble.
2) I restarted my android phone.
3) I opened my android browser and opened a flash web site.

And guess what?????? yay it worked………….not…………no it didn’t worked :P.
Browser just closed in between loading site. I tried with all browsers on my phone viz Opera, Skyfire, Dolphin HD, same result on all browser. All browser crashed.

So i guess flash player is not supported on my device. But i don’t care hehe 😛

Gracias.

I have been doing game development on android very recently (2 weeks) :D. And i bought samsung galaxy having android 2.1 to test my games and demos. I am pretty happy with the results and enjoying very much. Before this i had made games for j2me mobile devices and still making. Well anyway i am not discussing android game development here so i am coming to the primary focus of this post.

Physics. Yeah physics is phun and if decent physics is added to your game then it makes game more enjoyable, of course if gameplay design is good and fun. I have been playing with my own very very basic physics code(collision detection, collision response) that i have written for j2me. It is working fine in android after tweaking my code here and there. But i wanted more features so i thought to look for any decent free opensource physics engine for android. Unfortunately i didn’t get much success. There were some forums and tutorials where using Box2d with android was discussed. This site has shown the tutorial using JBox2d on android and is been discussed by the people there.

I am a big fan of Box2d. I have been using JBox2d and Box2d flash for my respective platform based game. Library had fulfilled my satisfaction and need. I have not tested JBox2d on android yet. I wanted to do few research on it before doing that. In few forums people complained that if using more than 5 bodies, it brings the FPS rate down and makes simulation very slow. I haven’t checked that but is going to write code to see if its true. I think it may depend on hardware device specification and capability because i have played Angry Birds on my samsung galaxy s and physics of game works just fine and smooth. I came across one site where they showed the graph on how much number of bodies/elements effect the FPS(frame rate) using Box2d and Chipmunk. Here it is.

So now i am going to write a simple code of falling polygons using Box2d on android and see the FPS and performance. I will post the results and my experience on next post ASAP.

Merry Christmas & Happy New Year
Gracias.