How to use MySQL on Android

23

After some research, I saw that the only database that Android can use is SQLite, is that correct?

Because I have an application that connects to an online database (MySQL) and would like to create an Android application that uses the same database, is it possible?

If yes, how can I connect to a base mysql ?

    
asked by anonymous 28.02.2014 / 14:44

3 answers

19

As is answered here , you can use an online database, in your case MySQL, as long as you have one:

  • Webservice
  • Client

Webservice You must apply to the bank and return a response to be processed, usually a JSON , which can be sent, for example , with as follows:

<?php
   echo json_encode($minha_info);
?>

This will return a JSON to the client.

Customer You can use the JSON package from Java itself , like this one example and retrieve the information on the client. The following example will use the reading of a twitter feed:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  String readTwitterFeed = readTwitterFeed();
  try {
    JSONArray jsonArray = new JSONArray(readTwitterFeed);
    Log.i(ParseJSON.class.getName(), "Number of entries " + jsonArray.length());
    for (int i = 0; i < jsonArray.length(); i++) {
      JSONObject jsonObject = jsonArray.getJSONObject(i);
      Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
}

public String readTwitterFeed() {
  StringBuilder builder = new StringBuilder();
  HttpClient client = new DefaultHttpClient();
  HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/vogella.json"); //Aqui ele pega o json do tutorial, nessa linha que o seu cliente vai declarar o webservice que enviará o json
  try {
    HttpResponse response = client.execute(httpGet);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
      HttpEntity entity = response.getEntity();
      InputStream content = entity.getContent();
      BufferedReader reader = new BufferedReader(new InputStreamReader(content));
      String line;
      while ((line = reader.readLine()) != null) {
        builder.append(line);
      }
    } else {
      Log.e(ParseJSON.class.toString(), "Failed to download file");
    }
  } catch (ClientProtocolException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return builder.toString();
}
    
28.02.2014 / 15:03
4

Felipe, actually works yes! I myself am developing a system for bars and restaurants with an app and I am using MySQL direct connection with Android, and, contrary to what most people say, it is not slow.

The only caveat is that the android API does not allow you to exchange data over the network (either Wi-Fi or 3G / 4G) using the main thread (UIThread). For this there are auxiliary classes like AsyncTask , present in the API , which assists in running background processes. All communication between the app and the bank should be done through a secondary thread, that is, a process in the background (including the connection itself).

I just forgot to post an example of how to implement the android connection to MySQL.

Looking at the net I found this tutorial very interesting and very similar to the way I use the connection.

android connection with MySQL using the AsyncTask class

I hope I have helped! Hugs

    
16.12.2014 / 20:12
0

You will have to build an API (Usually json / rest ) so that you can communicate with the server, and it treats the data for you.

    
28.02.2014 / 14:53