This is done using REST APIs. We described in detail about these REST API in our earlier tutorial. Here, let us discuss how to use them in an android project.
Here, we are going to load data in remote MySQL database into our android app. We use recyclerviews for this.
First of all, we need to create the backend REST APIs. I use Laravel framework for this. It follows the MVC architecture. We’ll discuss MVC architecture in another tutorial. For now, we can say it is a concept with three components namely, Model, View and Controller. We implement our REST API within a controller. With the below command we can create our controller.
php artisan make:controller AppController
Here, php artisan make:controller is the static part of the command and ‘AppController’ is the name I gave to my controller.
Then our controller is ready. Here is the code we implement within the controller. We can implement a method like this. It will then query out and return data in json format. ‘data’ is the name of the database table.
Then go to routes>>api.php. There we add the route like this.
The first parameter here is known as the endpoint. The second parameter contains the method name followed by the Controller name.
We have finished creating our REST API. Now we are going to create our android project.
Open android studio and start a new project like this.
After you create the project add the retrofit, gson converter, RecyclerView dependancies into the build.gradle (Module: app)
Then sync the project and there after all the necessary dependancies will be downloaded.
After that create the java class containing the getters and setters. Here we name it as Months.java
Then let us create the Interface to define the endpoints. In this sample app, we have only one endpoint named ‘/data’ .
Here we are going to get this data into recyclerviews. For that we need to bind the data coming from the API call. That is done using an adapter class. We’ll create that adapter class as DataAdapter.java
Next, we need to create a new class with a retrofit instance. We provide a BASE_URL which is relevant to the path of the implemented REST API. This class then is responsible for sending network requests to REST APIs using Retrofit.
Here when you provide the BASE_URL, if the database is in your local machine as in mine, use the current IP address and then the path to your Laravel project in htdocs. For me it is, Project>>MyTest1. MyTest1 is the name of my Laravel project. Then inside it we provide the path to our API, which is always ‘public/api/’
This is the layout file for showing data in RecyclerView using CardViews.
Next we create the MainActivity.java which is the main java file of our project. This contains the API call. The method enqueue() sends the request and notifies our app when a response is received. And also this handles what to do if request is failed. For that we implement two callback methods as onResponse() and onFailure().
Then this is the xml file with the recyclerview for the data. It is in the activity_main.xml file.
Now we have finished many parts of our app. Let us add the internet permissions in the AndroidManifest.xml file.
Now we have finished the implementation part of our app. Run it on an emulator or an android device. Then you can see how it is working….:)