Я использую retrofit2 для сетевых запросов на какой-либо ресурс.Ожидается соответствующий ответ:
{
"coach_details":[
{
"coach_id": 8,
"academy_id": 1,
"username": "coach2@gmail.com",
"first_name": "David",
"last_name": "test",
"gender": "male",
"email": "",
"mobile": "",
"middle_name": "",
"nick_name": "",
"state": "3"
}
],
"status": 1,
"message": "success"
}
or
{
"status": 0,
"message": "Oops! Invalid username or password!"
}
Это конечная точка входа в систему для моего приложения, и впоследствии я добавил DBFLow в качестве базы данных ORM.Я использую один и тот же класс модели для DBFlow и retrofit2, как показано ниже:
Класс Coach, который является основным ответом:
public class Coach {
private String status;
private String message;
@SerializedName("coach_details")
private CoachDetails coachDetails;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public CoachDetails getCoachDetails() {
return coachDetails;
}
public void setCoachDetails(CoachDetails coachDetails) {
this.coachDetails = coachDetails;
}
}
и Сведения о Coach, который является объектом для сохранения
@Table(database = SportsDatabase.class)
public class CoachDetails extends BaseModel {
@SerializedName("coach_id")
@PrimaryKey
@Column
public String coachId;
@SerializedName("academy_id")
public String academyId;
@SerializedName("username")
public String username;
@SerializedName("first_name")
public String firstName;
@SerializedName("last_name")
public String lastName;
@SerializedName("gender")
public String gender;
@SerializedName("mobile")
public String mobileNum;
@SerializedName("email")
public String emailAddr;
@SerializedName("middle_name")
public String midName;
@SerializedName("nick_name")
public String nickName;
@SerializedName("state")
public String originState;
public CoachDetails() {
}
public String getCoachId() {
return coachId;
}
public void setCoachId(String coachId) {
this.coachId = coachId;
}
public String getAcademyId() {
return academyId;
}
public void setAcademyId(String academyId) {
this.academyId = academyId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMobileNum() {
return mobileNum;
}
public void setMobileNum(String mobileNum) {
this.mobileNum = mobileNum;
}
public String getEmailAddr() {
return emailAddr;
}
public void setEmailAddr(String emailAddr) {
this.emailAddr = emailAddr;
}
public String getMidName() {
return midName;
}
public void setMidName(String midName) {
this.midName = midName;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public String getOriginState() {
return originState;
}
public void setOriginState(String originState) {
this.originState = originState;
}
public void insertInfo(String coach_id, String acadId, String username, String first, String last, String gender, String mobile, String mid_name, String nick, String state) {
this.coachId = coach_id;
this.academyId = acadId;
this.username = username;
this.firstName = first;
this.lastName = last;
this.gender = gender;
this.mobileNum = mobile;
this.midName = mid_name;
this.nickName = nick;
this.originState = state;
}
}
и в своей деятельности я попытался сохранить после сетевого вызова, как показано ниже, код, а также попытался проверить чтение из базы данных, но получить нулевые значения в журнале.Мое сохранение выполнено правильно или мой запрос на чтение неверен?Пример кода:
private void attemptLogin(String username, String pwd) {
Call<Coach> call = RestClient.getRestInstance()
.getLoginService()
.loginCoach("testcoach@gmail.com", "123@abcd"); // TODO: 12/17/2018 Use input values
call.enqueue(new Callback<Coach>() {
@Override
public void onResponse(Call<Coach> call, Response<Coach> response) {
if (response.isSuccessful()) {
Coach coach = response.body();
coach_id = coach.getCoachDetails().getCoachId();
String acadId = coach.getCoachDetails().getAcademyId();
String username = coach.getCoachDetails().getUsername();
String first = coach.getCoachDetails().getFirstName();
String last = coach.getCoachDetails().getLastName();
String gender = coach.getCoachDetails().getGender();
String mobile = coach.getCoachDetails().getMobileNum();
String email = coach.getCoachDetails().getEmailAddr();
String mid_name = coach.getCoachDetails().getMidName();
String nick = coach.getCoachDetails().getNickName();
String state = coach.getCoachDetails().getOriginState();
// Log.d(TAG, "State:\t" + state);
CoachDetails coachDetails = new CoachDetails();
// coachDetails.insertInfo(coach_id, acadId, username, first, last, gender, mobile, mid_name, nick, state);
coachDetails.setCoachId(coach_id);
coachDetails.setAcademyId(acadId);
coachDetails.setFirstName(first);
coachDetails.setLastName(last);
coachDetails.setGender(gender);
coachDetails.setMobileNum(mobile);
coachDetails.setEmailAddr(email);
coachDetails.setMidName(mid_name);
coachDetails.setNickName(nick);
coachDetails.setOriginState(state);
coachDetails.save();
// CoachDetails details = SQLite.select()
// .from(CoachDetails.class)
// .querySingle();
// Log.d(TAG, "CId from db:\t" + details.getCoachId() + " first_name:\t" + details.getFirstName()); // name is null and id is showing properly
// startActivity(new Intent(LoginActivity.this, MainActivity.class));
} else {
Snackbar.make(findViewById(android.R.id.content), "Login Failed with Error Code:\t" + response.code(),
Snackbar.LENGTH_LONG).show();
loginBtn.setEnabled(true);
}
}
@Override
public void onFailure(Call<Coach> call, Throwable t) {
Snackbar.make(findViewById(android.R.id.content), "Unable to Login...Check Your Connection or Credentials",
Snackbar.LENGTH_LONG).show();
}
});
}
Может кто-нибудь указать, где что-то идет не так?