views:

286

answers:

5

Is there any sample?

I have my android application and I need to connect to mysql server on my machine, what is the best way?

I should not use jdbc, explanation here link text

And should go for:

DefaultHttpClient httpclient = new DefaultHttpClient(); 

But there is no example in how to open a connection or excute a simple sql statement. anyone could help me?

Regards,

A: 

You won't be able to connect directly to a MySQL database with the HttpClient, as the MySQL database doesn't operate on that protocol.

The second part of the answer to the question you linked recommends going with web services and consuming those to communicate with the database server. Which is exactly what you would be able to do with the HttpClient.

ShaneB
So it's no possible to connect to Mysql using HttpClient, so what should use instead if I neither can use jdbc nor HttpClient?
Alex
You can use HttpClient you just need to write a web application that can process the information between the mobile device and the database. A RESTful web service would do what you want. http://www.ibm.com/developerworks/webservices/library/ws-restful/
ShaneB
but, it seems that I need one webserver as middle tier, is that correct?
Alex
+1  A: 

In order to connect to a MySQL server, you need a MySQL client. Android does not come with any MySQL libraries. You may be able to take a generic Java MySQL library and fudge it to work with Android, but that would be a big undertaking and wasted time.

The link you pointed to already told you that what you're trying to do is wrong in the first place. Don't connect to a database across the internet! You will need something on your server that responds to HTTP requests, looks up data in the database, and sends them back via HTTP. The link already mentioned a few options. You could even write something yourself, although it's most likely easier to use an existing solution than trying to make your own approach safe and hack-proof.

EboMike
A: 

You should either use web services or implement an HTTP handler and transfer in a RESTful manner.

Josh Bolton
A: 

If you need to share data between multiple phones, I would recommend exposing it as a web service. If the data only needs to be accessible on that particular phone (and you want to remain relational) you can use sqlite.

Octoberdan
I need to transfer from phone to database
Alex
A: 

Alex, you obviously need to educate yourself little more before asking this kind of questions. People here gave you the 2 possible solutions and you still don't get it!

Dan