Please I'm stock using retrofit for few days, I'm trying to parse the response to my POJO class, initially the error was setlenient to true wich i did and ended up here, no related questions here seem to solving my problem
JSON Response from server
{
"error": false,
"message": "Login successful",
"student": {
"fullname": "Idris Muhammad",
"matric": "u14/fns/csc/066",
"department": "computer science",
"gender": "male",
"email": "mail4idris@gmail.com",
"phone": "08065468035",
"level": 100,
"faculty": "Applied Science and Technology"}
}
POJO Class public class LoginResponse {
@SerializedName("error")
private boolean error;
@SerializedName("message")
private String message;
@SerializedName("student")
private Student student;
public LoginResponse(boolean error, String message, Student student) {
this.error = error;
this.message = message;
this.student = student;
}
public boolean isError() {
return error;
}
public String getMessage() {
return message;
}
public Student getStudent() {
return student;
}
}
Restrift Interface
@FormUrlEncoded
@POST("login")
Call<LoginResponse> studentLogin(
@Field("matric") String matric,
@Field("password") String password
);
My OnclickListener
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Call<LoginResponse> call = RetroClient.getInstance().getAPI().studentLogin(id.getText().toString(),password.getText().toString() );
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
LoginResponse loginResponse = response.body();
if (loginResponse.isError()){
Toast.makeText(getBaseContext(), "Error", Toast.LENGTH_LONG).show();
return;
}else {
String matnum []= id.getText().toString().split("/");
SharedPrefManager sharedPrefManager = SharedPrefManager.getInstance(getBaseContext());
sharedPrefManager.storeUserType(SharedPrefManager.USER_TYPE_STUDENT);
sharedPrefManager.storeDepartment(matnum[2]);
sharedPrefManager.storeFaculty(matnum[1]);
sharedPrefManager.storeLevel(loginResponse.getStudent().getLevel());
sharedPrefManager.storeActivation(true);
sharedPrefManager.storeUserType(SharedPrefManager.USER_TYPE_STUDENT);
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Toast.makeText(getBaseContext(), "Error "+t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
});
This is my Student class
public class Student {
@SerializedName("fullname")
private String fullname;
@SerializedName("matric")
private String matric;
@SerializedName("department")
private String department;
@SerializedName("gender")
private String gender;
@SerializedName("email")
private String email;
@SerializedName("phone")
private String phone;
@SerializedName("level")
private String level;
@SerializedName("faculty")
private String faculty;
public Student(String fullname, String matric, String department, String gender, String email, String phone, String level, String faculty) {
this.fullname = fullname;
this.matric = matric;
this.department = department;
this.gender = gender;
this.email = email;
this.phone = phone;
this.level = level;
this.faculty = faculty;
}
public String getFullname() {
return fullname;
}
public String getMatric() {
return matric;
}
public String getDepartment() {
return department;
}
public String getGender() {
return gender;
}
public String getEmail() {
return email;
}
public String getPhone() {
return phone;
}
public String getLevel() {
return level;
}
public String getFaculty() {
return faculty;
}
}
my logcat
2018-11-29 14:12:59.244 4775-4775/com.ietech.ibbumobile E/RETROFIT: JSON document was not fully consumed.