How to Build a Webservice to Synchronize Android and Mysql Sqlite Database

Asked

Viewed 6,498 times

5

I built an android app that stores basic information about a contact in Sqlite:
Name, Telephone, E-mail, Address among others.

I searched a little and from what I understood, what I can do is use a Webservice, to receive data from Sqlite, treat them and insert them into the database using PHP.

I have 2 questions:

1- How I create Webservice?

2- What do I need to carry out the communication between it and the database on android? Any library in particular?

1 answer

6


You can use the combination JSON API + Retrofit. Explaining a little:

  • You must create a data pattern so that both your application and your remote database can "understand" the information that is shared between them. This standard can be created using JSON, because both PHP and Android can read JSON files ( using their proper libraries);

  • You must create a JSON API, which will be the standard of communication between your application and your remote database, that is, you must create a "language" of communication that PHP and Android will use to request and send data between themselves;

  • In PHP, it’s very simple to read and send data in JSON, but on Android you’ll need a library that reads and sends JSON data from a remote database. This library could be Retrofit.

A small example:

  • Data standard: suppose the customer data is represented in this way in JSON

    {
        "cliente": {
            "nome": "joao",
            "email": "emaildojoao",
            "endereco": "enderecodojoao"
        }
    }
    
  • API JSON: now suppose you have created a page customers on its website and every time it was accessed it would return a JSON array with all the clints of its database, therefore, every time the application wanted to access the client list of the remote database, it would need to access the link "/seusite/clients".

  • Using Retrofit: In your app,using Retrofit, you could write the following code so that your application could pick up the website’s customer list

    @GET("/users/list")
    List<Cliente> listaDeClientes();
    

    In that case, when you called the function listaDeClientes(), it would automatically return a list of Clientes from your website.

Remembering that this is just one of many solutions. If you are interested in this, do a more thorough search and when you have more specific questions, just come back here.

  • Thank you very much, that’s exactly what I’ve been looking for. I started using Gson to convert objects to Json, now I will study Retrofit to communicate with the server.

Browser other questions tagged

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