我正在使用 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 使用相同的模型类,如下所示:
教练班是主要反应:
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;
}
}
和教练详细信息,这是要保存的对象
@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();
}
});
}
有人可以指出哪里出错了吗?