Android MySQL Retrofit Full Course – INSERT SELECT UPDATE DELETE SEARCH PAGINATION → Creating Model and ResponseModel Classes – Camposha

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

Creating Model and ResponseModel Classes

Introduction

In this lesson we will define the model classes we will need for this project. Remember this is part of Android Live Courses Season 1 – Creating a Scientists App based on MySQL CRUD series. In this lesson we create two model classes:

(a). Scientist.java

This is our data object. It will represent our Scientist object. The scientist will have properties, defined by instance fields.

Start by creating a class called Scientist in Scientit.java file:

import com.google.gson.annotations.SerializedName;
import java.io.Serializable;

 /**
 * Let's Create our Scientist class to represent a single Scientist.
 * It will implement java.io.Serializable interface, a marker interface that will allow
 *  our
 * class to support serialization and deserialization.
 */
public class Scientist implements Serializable {

As you can we have made it serializable by implementing the java.io.Serializable interface. Thus we will be able to pass it across activities easily then deserialize it.

Then go ahead and define our instance fields:

    /**
     * Let' now come define instance fields for this class. We decorate them with
     * @SerializedName
     * attribute. Through this we are specifying the keys in our json data.
     */
    @SerializedName("id")
    private String mId;
    @SerializedName("name")
    private String name;
    @SerializedName("description")
    private String description;
    @SerializedName("galaxy")
    private String galaxy;
    @SerializedName("star")
    private String star;
    @SerializedName("dob")
    private String dob;
    @SerializedName("died")
    private String died;

As you can see we have annotated the various fields via the @SerializedName attribute. There we pass our JSON data’s keys. Remember Retrofit will be using GSON to map this model class into our JSON Objects. Thus we need to specify the json keys to be mapped to specific fields.

Then we add our getters and setters:

    /**
     * Let's now come define our getter and setter methods.
     */
    public String getId() {
        return mId;
    }

    public void setId(String id) {
        mId = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getStar() {
        return star;
    }

    public void setStar(String star) {
        this.star = star;
    }

    public String getGalaxy() {
        return galaxy;
    }

    public void setGalaxy(String galaxy) {
        this.galaxy = galaxy;
    }

    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getDied() {
        return died;
    }

    public void setDied(String died) {
        this.died = died;
    }

    @Override
    public String toString() {
        return getName();
    }
}
//end

Video Lesson(Recommended)

(b). ResponseModel

This will represent our response model. We are defining a class to represent the data we will be getting from the server. That data will comprise the following:

No. Name Description Example
1. Code The response Code. 1
2. Message The response message SUCCESS
3. List The List of scientist objects List<Scientist> scientists

Here’s the full code:

package info.camposha.retrofitmysqlcrud.Retrofit;

import com.google.gson.annotations.SerializedName;
import java.util.List;

/**
 * Our json response will be mapped to this class.
 */
public class ResponseModel {
    /**
     * Our ResponseModel attributes
     */
    @SerializedName("result")
    List<Scientist> scientists;
    @SerializedName("code")
    private String code;
    @SerializedName("message")
    private String message;

    /**
     * Generate Getter and Setters
     */
    public List<Scientist> getResult() {
        return scientists;
    }

    public void setResult(List<Scientist> scientists) {
        this.scientists = scientists;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
//end

Leave a Reply

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

(2) Comments

  • IOSTREAM17 July 5, 2019 @ 6:55 am

    good job well done

    • Admin bar avatar
      Oclemy July 5, 2019 @ 7:08 am

      Thank you very much.

Comments are closed.

X