how to connect android to mysql (external DB)?

2

Friends I am creating an app and I need to consult data from my host, so I have read this obsolete so far using the apache httpcient library now I must use threads, I'm not lost, any help or tutorial. first of all, Thanks!!! and eye is my first question

    
asked by Robin958 20.04.2016 в 18:59
source

4 answers

4

I think the safest and most scalable way is to offer a services API on a server and this last one that connects to the database.

  • Safer because it is not advisable to leave the port to the database open.
  • More scalable because on a server you will have more resources to solve requests from API clients.

You can also consume the API from another type of client, such as a website.

I am currently in the development of an application that connects to a MySQL database and from Android I use retrofit2 to consume the API and the server we are developing it with nodejs using the module node-mysql .

I hope you get the answer.

    
answered by 21.04.2016 в 01:03
2

Regarding the class for connection org.apache.http.client.HttpClient

strong> as you comment, is currently obsolete, if you still need to use the class HttpClient , you can add this dependency in your build.gradle :

android {
    useLibrary 'org.apache.http.legacy'
}

Although it is advisable to use the class HttpURLConnection if your target is Android 2.3 or higher (which is probably the case).

Example:

   URL url = new URL("http://www.android.com/");
   HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
   try {
     InputStream in = new BufferedInputStream(urlConnection.getInputStream());
     readStream(in);
    finally {
     urlConnection.disconnect();
   }
 }

To connect to an external database, you can do it through a Web Service ( Web Service ):

This is a tutorial in Spanish of an Android application, that consumes a Web Service using HttpURLConnection, I hope it helps:

Consume Web Service on Android in a simple way

    
answered by 20.04.2016 в 21:54
0

Good day, I agree with the answer made by Pablo, the best thing is that you create your restfull services. If you use PHP you can use Sublime that will help you create a restfull service quickly. I have also had the opportunity to use retrofit2 with rxjava. link It's very useful when it comes to keeping the value of a request at your service.Greetings

    
answered by 21.04.2016 в 16:44
0

Hello dear, I would recommend that you use the Volley library that is the official google library (Android) to make mysql server requests through php generating xml, I used it to connect to a Bd Mysql, to consult data and at the same time make changes (Insert, Update) I leave a link for you to see a tutorial that served me: link

    
answered by 01.06.2016 в 17:44