connectivity with mysql

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

connectivity with mysql

Postby schofeild » Thu Sep 09, 2010 5:59 pm

hey guys i am developing a final year location-based project(gps) in android.I have to create a server using php and mysql which contains name and location(in latitude and longitude) of important hospitals in Mumbai.The android client should be able to retrieve the the latitude and longitude of locations in mysql(external) database and then plot it on a google map.

Could u plzz tell me whether this is possible?If yes, then how?
Any help would be appreciated....cheers
schofeild
Developer
Developer
 
Posts: 40
Joined: Wed Jul 21, 2010 12:08 pm

Top

Re: connectivity with mysql

Postby nickt » Mon Sep 13, 2010 9:34 pm

It's quite easy to produce something that proves the concept. I did it in a couple of hours knowing no php.
Firstly create a mysql database on the server, 1 table (hospitals), 3 columns (name (primary key), latitude, longitude). Populate it with a couple of rows. I have 2 hospitals in England:

Longitude Latitude Name
-2.929010000000000 53.464649999999999 Aintree
-0.266850000000000 51.531359999999999 Middlesex


Then put the php file getposition.php below in your apache documents root:

<?php
mysql_connect("127.0.0.1","YOUR_USER_ID","YOUR_PASSWORD"); // your set up
mysql_select_db("test"); // your db name
$hname=$_GET["hospname"]; // hospname - parameter that will be passed
// in by android app
$query="select * from hospitals where name = '$hname'";
$rs = mysql_query($query);
$row = mysql_fetch_assoc($rs);
?>
<?php
echo $row['Longitude'];
?>
<?php
echo "\r\n";
echo $row['Latitude'];
?>

You can adapt the demo program WikiWhere (Google for it). It has only one class!
Add an EditText box and make the button have a ClickHandler
Set a base URL string
final String urlBase = "http://192.x.x.x/testPHP/getposition.php?hospname=";
// 192.etc is what the android app sees as my machine (set by my router)

In the click handler append whatever you've entered in the textbox to the above.
using: URL targetURL = new URL(urlBase + urlBox.getText().toString());

Read in text from the URL, it should just be numerics separated with \r\n, by:

InputStream isText = targetURL.openStream();
byte[]bText = new byte[300];
int readSize = isText.read(bText);

You can use:
HttpURLConnection http = (HttpURLConnection)targetURL.openConnection();
before the above and check the response code and ContentLength to know how much to read and to catch errors

Once you've done this, strip out the lat/lon from the text and convert to microdegrees (long west = -ve number) before setting:
mc.setCenter(new GeoPoint(lat, lon)); with the new values and calling mv.invalidate().

You'll also need to add a Timer thread to call a message handler in the usual manner.

You'll need to catch all the IOExceptions and cater for URL not found, php file not found if you want good marks, but this at least will prove it's possible
nickt
Freshman
Freshman
 
Posts: 6
Joined: Mon Feb 22, 2010 1:05 pm

Re: connectivity with mysql

Postby schofeild » Thu Sep 16, 2010 4:24 pm

hey thanx a lot nickt...really helpful information
just lemme know which web service is d best option to bes used for connecting android to a server
rest or soap??
actually i do not have any knowledge(practically) pertaining to such services...
schofeild
Developer
Developer
 
Posts: 40
Joined: Wed Jul 21, 2010 12:08 pm

Re: connectivity with mysql

Postby nickt » Thu Sep 16, 2010 4:54 pm

I don't know anything about SOAP I'm afraid. I just used the standard Apache web server, see:

http://httpd.apache.org/index.html

I installed that on my only machine, together with PHP and just had it listening on port 80. (You can install it on the remote machine with the mysql database).

I edited/added the following lines in httpd.conf (In the Apache's conf folder):

Listen 80

LoadModule php5_module "c:/dev/tools/PHP/php5apache2_2.dll"
AddHandler application/x-httpd-php .php
# configure the path to php.ini
PHPIniDir "c:/dev/tools/PHP"

Then I tested it by entering "http://localhost/" in my Firefox/IE browser (you put in the name/ip of your server)
It should return a web page the says"It works!")
nickt
Freshman
Freshman
 
Posts: 6
Joined: Mon Feb 22, 2010 1:05 pm

Re: connectivity with mysql

Postby schofeild » Sat Sep 18, 2010 5:32 pm

hey nickt thanx a lot... 8)
schofeild
Developer
Developer
 
Posts: 40
Joined: Wed Jul 21, 2010 12:08 pm

Re: connectivity with mysql

Postby schofeild » Sat Oct 23, 2010 4:50 am

hey nickt,
could u plzz suggest me some good tutorials (basics)for learning client server communication using apache (http client and http server) on the web...and also if possible its implementation in android(so that i can use it in my final project to connect to my php-mysql server ) ....in order to make it more efficient... :wink:
schofeild
Developer
Developer
 
Posts: 40
Joined: Wed Jul 21, 2010 12:08 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: Google [Bot] and 7 guests