How to connect my android application to an external database

Asked

Viewed 3,185 times

0

I have a site with some tables in the mysql database. The site is already connected in the database.

I have an application in Android Studio and would like to connect the application with the external database (mysql on the site). Other than SQLITE. How can I make that connection? multiplatform

  • If I’m not mistaken, there’s a duplicate of this somewhere, but I can’t seem to find it. Well, the recommended in this case is your Android application does not connect with the bank directly and rather call web services that access this bank. If you already have a website connected to the bank, could implement web services on this site to make this communication.

3 answers

0


First of all, you will need a connection class, I use this in my projects with help with php:

public class Conexao {

public static String postDados(String urlUsuario, String parametrosUsuario) {
    URL url;
    HttpURLConnection connection = null;

    try {

        url = new URL(urlUsuario);
        connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");

        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");

        connection.setRequestProperty("Content-Lenght", "" + Integer.toString(parametrosUsuario.getBytes().length));

        connection.setRequestProperty("Content-Language", "pt-BR");

        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        OutputStreamWriter outPutStream = new OutputStreamWriter(connection.getOutputStream(), "utf-8");
        outPutStream.write(parametrosUsuario);
        outPutStream.flush();
        outPutStream.close();

        InputStream inputStream = connection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

        String linha;
        StringBuffer resposta = new StringBuffer();

        while((linha = bufferedReader.readLine()) != null) {
         resposta.append(linha);
            resposta.append('\r');
        }

        bufferedReader.close();

        return resposta.toString();

    } catch (Exception erro) {

        return  null;
    } finally {

        if(connection != null) {
            connection.disconnect();
        }
    }
}
}

Ready, now to use it you do it:

public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String texto = "123";
    url = "url_do_seu_arquivo_php"; //Caso queira um get, só colocar na url
    //  ?variavel=valor

    //Parâmentros por post
    parametros = "texto=" + texto;

    new SolicitaDados().execute(url);

 }

 private class SolicitaDados extends AsyncTask<String, Void, String> {
    @Override
    protected  String doInBackground(String... urls) {

        return Conexao.postDados(urls[0], parametros);

    }

    @Override
    protected void onPostExecute(String resultado) {
        //String resultado tem o retorno


    }
 }
 }

So you send the posts and gets to your php file, for example, and there treats you by sending the data to mysql, if you want to receive something, in the url you put the path with php file and passes the necessary parameters for this print the data, the whole answer comes in String "reply" on onPostExecute

0

You can use Firebase for this, in addition to database it offers you possibility of authentication of users, pushNotifications and several other tools, and in addition it has a very generous Free plan, so that you will not need to spend anything at first. Besides being cross-platform.

-2

Mysql I don’t think that’s possible. There is a google tool called Firebase It is not quite a database but can serve for what you need.

  • 1

    Of course it is possible

  • @Wotonsampaio I said I think it’s not possible I’ve been trying for a while and I haven’t found any way to do it. But THINKING IT’S NOT POSSIBLE Doesn’t mean I’m right

Browser other questions tagged

You are not signed in. Login or sign up in order to post.