0

I am using gson library in my demo android project. I want to know should I create a different model for login request & login response in json.

For the request I created like below

public class Login {

    private String username;
    private String password;

    public String getUserName() {
        return username;
    }

    public void setUserName(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Creating JSON from login request.

// creating JSON using GSON library
Login mLogin = new Login();
mLogin.setUserName(userName);
mLogin.setPassword(password);

Gson gson = new GsonBuilder().create();
String loginJSON = gson.toJson(mLogin);

So here above I created a model for login request & should I create a new model of login response which again in json or Am I not doing following correct approach.

Cœur
  • 37,241
  • 25
  • 195
  • 267
N Sharma
  • 33,489
  • 95
  • 256
  • 444

1 Answers1

2

Gson helps us to serialize and deserialize json objects/strings to make our coding life easier. If you want you can write your own json parser or use another json parsers such as jackson. Also you can work on pure string value of jsons because json is just a formatted string like xml representation.

Pojos for json values make coding, reading code, maintenance easier. I prefer having pojo classes for both input and output json values.(that is just a choice)

I created a model for login request & should I create a new model of login response which again in json or Am I not doing following correct approach.

The answer of your question is; it's up to you. If you really need a pojo class then you should write it. If you don't then you may prefer to write it or handle json without any pojos. (By using Gson you can get/create json arrays, objects, values.)

You can see my previous answer for another question to be clear.

Community
  • 1
  • 1
Devrim
  • 15,345
  • 4
  • 66
  • 74