6

I am starting my thesis which is an app for android. This app is based on a web platform I had created.

The part where I need advice is: which is the most efficient way to pull data from a MySQL server into the application. Please give me some advice and your experience on the matter.

(I've read about encoding the queries in json, but it seems like awful lot of needless work)

MayTheSchwartzBeWithYou
  • 1,181
  • 1
  • 16
  • 32
  • 2
    Accessing external data is usually done with some HTTP based API (since there is builtin HTTP support in Android / Java) & a data encoding format with good API support, one of those would be JSON. But you can use any "transfer data over the internet" approach you can think of. Even directly speaking to the database server if you like the risk of exposing your db to the net. – zapl Aug 14 '12 at 15:35
  • I can think of two ways: i) SOAP and ii) json. Are there any other alternatives? – MayTheSchwartzBeWithYou Aug 14 '12 at 15:51
  • 1
    json all the way, baby. There's a built-in class on Android to parse it. When I've done this, I wrote python scripts on the server side to generate the json. There's probably libraries for that too. – Edward Falk Aug 14 '12 at 17:57
  • FWIW, I've also written java (not Android) apps to directly connect to remote MySQL servers. It's not worth the effort since the SQL library is an external library you have to bring into your app. I wound up switching to Python/json. – Edward Falk Aug 14 '12 at 17:59
  • Thank you. It seems interesting. I will train myself to json then – MayTheSchwartzBeWithYou Aug 14 '12 at 18:51

2 Answers2

7

I suggest you to use RESTful Web Service in Java using Jersey as an intermediate layer between you Android App and MySQL server. You can transfer data in JSON (my suggestion for a mobile app), xml or palin text to your Android App.

You can find the benefits of using Web Service in you system in @Elad answer : Best way to access a remote database: via webservice or direct DB-access?

Also later if you decide to develop other smart phone platform for your system, you just need to reuse the same Web Service. As a result this Web service can be considered as a generic protocol for the mobile user of your system.

I used Hibernate to map the data to MySQL database. RESTful Service Using Jersey with Hibernate Persistence

If you decide to follow this approach note that it is highly recommended to separate your hibernate stuff form your Jersey services. You need to wire your DAO to your Service tier. see what @Rick Mangi wrote to me : REST with Java (JAX-RS) using Jersey and hibernate

It is also good approach to use HTTP Client in your Android App, Since it supports @GET, @POST, @DELETE and @PUT commands and you can easily talk to your database like HTTP GET Request

Community
  • 1
  • 1
Ali
  • 9,800
  • 19
  • 72
  • 152
3

Perhaps, by using a MySQL Java connector, and directly connect to the server over port 3306 using the Java's JDBC connector as a Jar library imported into your Android project, as shown here, and also this was blogged a good while ago

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • Hm the second does not appeal to me. The first it seems that it copies the SQL database to the local sqli Android db. That could be a good approach, but it exposes the infrastructure of the database. It is not that bad if you change the infrastructure for this specific purpose. I will give it a thought. Thanks! – MayTheSchwartzBeWithYou Aug 14 '12 at 16:06
  • 1
    To clarify, both linkys, especially the second linky, demonstrates how to set up a mysql on the server side, and shows how to access it from Android. There's no hint of copying any database to the android side :) – t0mm13b Aug 14 '12 at 16:19