Android MySQL Retrofit Full Course – INSERT SELECT UPDATE DELETE SEARCH PAGINATION → Creating a REST API Interface – Camposha

Android MySQL Retrofit Full Course – INSERT SELECT UPDATE DELETE SEARCH PAGINATION

Creating a REST API Interface

Our REST API Interface

We will be making HTTP Calls to our server via our HTTP Client. That HTTP Client is Retrofit in this case. Retrofit is the perfect fit for our project because it’s both fast, convenient to use and well documented. It’s convenient since it saves us from having to manually parse our JSON data when we download it, or encode our Form data to be sent to the server.

Through Retrofit, we only need to:

  1. Create an interface to map to our API methods.
  2. Instantiate Retrofit.
  3. Enqueue a Call.

In this lesson we will do the first: creating an interface with methods that map to our server side API methods.

The second part, the instantiation of retrofit, occurs in the Utils class while the last part, the enqueing of our Calls will take place in our ScientistsActivity class and the CRUDActivity class.

Let’s start.

Video Tutorial(Recommended)

First create a java file called RestApi.java.

Then add our imports:

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;

Then define our interfce:

public interface RestApi {

(a). Making a HTTP GET Request

Let’s start by defining our first API method. Add it inside the interface:

    @GET("index.php")
    Call<ResponseModel> retrieve();

You can see above we’ve defined an abstract method called retrieve(). The method doesn’t take any parameters. It instead returns a retrofit2.Call object of generic parameter ResponseModel. It is also decorated with the @GET attribute. We specify the target URL in that attribute. In this case we’ve defined index.php, which is a just a path. Remember we had defined the base part of the URL in the Utils class.
THe above method will make a HTTP call to the server and download all our scientists.

(b). Inserting Data

Go ahead and add the following:

    @FormUrlEncoded
    @POST("index.php")
    Call<ResponseModel> insertData(@Field("action") String action,
                                   @Field("name") String name,
                                   @Field("description") String description,
                                   @Field("galaxy") String galaxy,
                                   @Field("star") String star,
                                   @Field("dob") String dob,
                                   @Field("died") String died);

Above we’ve defined a method called insertData(). That method is taking several parameters include one we’ve called action. The above parameters, apart from the action will be inserted into our mysql database. The action is just a string to identify our action, like in this case we are telling our server that this HTTP POST request is supposed to insert data to the database.

Again the response will be mapped to the ResponseModel class. Note that the method is actually returning a a retrofit2.Call object which will then be enqueued for us to asynchronously receieve a response.

You can also see that we have supplied two attributes: @FormUrlEncoded and @POST. In short we will be encoding our fields then making a HTTP POST request to the server.

(c). Updating Data

Now add the following method:

    @FormUrlEncoded
    @POST("index.php")
    Call<ResponseModel> updateData(@Field("action") String action,
                                   @Field("id") String id,
                                   @Field("name") String name,
                                   @Field("description") String description,
                                   @Field("galaxy") String galaxy,
                                   @Field("star") String star,
                                   @Field("dob") String dob,
                                   @Field("died") String died);

Again we are making a HTTP POST request. We’ve again decorated our method with the @FormUrlEncoded and @POST attributes. And again we will be receiving a response of type ResponseModel.The method is actually returning a Call object. The action in the parameters will identify this method’s action to the server. The id will specify the row we want to update.

(d). Searching MySQL Data

Now add the following code:

    @FormUrlEncoded
    @POST("index.php")
    Call<ResponseModel> search(@Field("action") String action,
                               @Field("query") String query,
                               @Field("start") String start,
                               @Field("limit") String limit);

Again we are making a HTTP POST request. This time our intention is to search data.The action parameter will specify that intention. The query will specify our search term. Our search filter will be supporting pagination. The start parameter will hence define our offset while the limit will specify the number of rows to be returned.

(e). Deleting Data

Lastly add the following:

    @FormUrlEncoded
    @POST("index.php")
    Call<ResponseModel> remove(@Field("action") String action, @Field("id") String id);
}

The above method will allow us delete our data from our mysql database. We onlu need to specify the action as well as the id. The id will identify the row we intend to remove.

Full Code

Here’s the full code:

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress › Error

The site is experiencing technical difficulties.