Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

How can i connect my MYSQL database on phpmyadmin to my android app in android studio?

I have implemented the searchView widget in android studio and I'd like to connect it to my database so that when i carry out a search, the results returned are form the database.

1 Answer

Víctor Hernández
Víctor Hernández
12,276 Points

You can't connect directly MySQL with your android app. Instead of it, you can write an API to expose your database information through REST web services. As you have phpmyadmin, I think that you can develop some php web services.

I have never used REST web services before, Is there any documentation or tutorials that can help me in writing the code for this?

Víctor Hernández
Víctor Hernández
12,276 Points

For PHP you can use some framework like http://www.recessframework.org/ or http://www.slimframework.com/.

If you don't want to use a framework you can use the json_encode function.

The goal is to build some php code that retrieves information from your mysql database and return it in JSON format, and then you can invoke your functionality through an URL making a network connection inside your android app.

Your php app will be the backend, and your android app will be your frontend.

You can follow the course: http://teamtreehouse.com/library/build-a-weather-app to learn how to call a rest web service from android and parse the json response.

Hi Victor, So i have gone through the 'Build a weather app' and i slightly understand what i have to do. Correct me if I'm wrong but to my understanding, if i compare my app to the one in this tutorial, my android app will be retrieving data from my database (or in the case of the tutorial, the weather API) through making a HTTP call and getting its response returning the data in JSON format?

Víctor Hernández
Víctor Hernández
12,276 Points

Hi Rachelle, Yes, it is correct. In more detail, You need to build your own API. I think that your database is used too by a web app developed with php. In your web app you will build some resource that allows retrieve your information in JSON through a HTTP GET call, for example http://webapp.rachelle.com/users/all and the response of it will be the json data of users (in this example), the same data that you parse in your android app. Finally, with these data refresh a list or any other component.

If you need some help, only write me.

My database is on phpmyadmin. So would i need to create my own API for that? If so, i am not quite sure where to start?

Víctor Hernández
Víctor Hernández
12,276 Points

I have one question. You say that your db is on phpmyadmin, do you have some php web app that connects with this database?

What do you mean by web app?

Víctor Hernández
Víctor Hernández
12,276 Points

A web app is some application that runs in web environment, you can build it using php or java for example, then you can build a web app to connect with your database and expose the information through a rest web service.

Contact me on Twitter or Facebbok in order to know your requirements with more detail and I will be happy to help you.