我正在试图弄清楚为什么我在使用改装登录后获得了一个空用户。这是我得到的回应:
{"status":{"code":0,"message":"Ok."},"result":{"user":{"id":29366,"first_name":"Pinch","last_name":"oijoiji","alias_exists":true}}}
但是在改造后解析它我得到的所有用户字段都为空。这是用户类:
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.SerializedName;
public class User implements Parcelable {
public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {
public User createFromParcel(Parcel in) {
return new User(in);
}
@Override
public User[] newArray(int i) {
return new User[i];
}
};
@SerializedName("id")
private String mId;
@SerializedName("first_name")
private String mFirstName;
@SerializedName("last_name")
private String mLastName;
@SerializedName("login")
private String mEmailAddress;
@SerializedName("msisdn")
private String mPhoneNumber;
@SerializedName("plate")
private String mCarRegistrationNumber;
private String mLanguageCode;
private String mCountryCode;
@SerializedName("alias_exists")
private boolean mHasBankingInformation;
public User() {
}
public User(Parcel parcel) {
mId = parcel.readString();
mFirstName = parcel.readString();
mLastName = parcel.readString();
mEmailAddress = parcel.readString();
mPhoneNumber = parcel.readString();
mCarRegistrationNumber = parcel.readString();
mLanguageCode = parcel.readString();
mCountryCode = parcel.readString();
mHasBankingInformation = parcel.readByte() == 1;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(mId);
parcel.writeString(mFirstName);
parcel.writeString(mLastName);
parcel.writeString(mEmailAddress);
parcel.writeString(mPhoneNumber);
parcel.writeString(mCarRegistrationNumber);
parcel.writeString(mLanguageCode);
parcel.writeString(mCountryCode);
parcel.writeByte((byte) (mHasBankingInformation ? 1 : 0));
}
// Basic methods
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof User)) {
return false;
}
User user = (User) o;
return mId.equals(user.mId);
}
@Override
public int hashCode() {
return mId.hashCode();
}
// Accessors
/**
* This mId can be used to uniquely identify the user on remote servers.
*
* @return User's unique mId.
*/
public String getId() {
return mId;
}
public void setId(String id) {
this.mId = id;
}
/**
* @return User's first name.
*/
public String getFirstName() {
return mFirstName;
}
public void setFirstName(String firstName) {
this.mFirstName = firstName;
}
/**
* @return User's last name.
*/
public String getLastName() {
return mLastName;
}
public void setLastName(String lastName) {
this.mLastName = lastName;
}
/**
* @return User's email address.
*/
public String getEmailAddress() {
return mEmailAddress;
}
public void setEmailAddress(String emailAddress) {
this.mEmailAddress = emailAddress;
}
/**
* @return User's mobile phone number.
*/
public String getPhoneNumber() {
return mPhoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.mPhoneNumber = phoneNumber;
}
/**
* @return User's car registration number (vehicle plat).
*/
public String getCarRegistrationNumber() {
return mCarRegistrationNumber;
}
public void setCarRegistrationNumber(String carRegistrationNumber) {
this.mCarRegistrationNumber = carRegistrationNumber;
}
/**
* @return User's preferred language ISO code.
*/
public String getLanguageCode() {
return mLanguageCode;
}
public void setLanguageCode(String languageCode) {
this.mLanguageCode = languageCode;
}
/**
* @return User's country ISO code.
*/
public String getCountryCode() {
return mCountryCode;
}
public void setCountryCode(String countryCode) {
this.mCountryCode = countryCode;
}
/**
* @return Whether the user has defined banking information (credit card) for his account.
*/
public boolean getHasBankingInformation() {
return mHasBankingInformation;
}
public void setHasBankingInformation(boolean hasBankingInformation) {
this.mHasBankingInformation = hasBankingInformation;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("User{");
sb.append("mId='").append(mId).append('\'');
sb.append(", mFirstName='").append(mFirstName).append('\'');
sb.append(", mLastName='").append(mLastName).append('\'');
sb.append(", mEmailAddress='").append(mEmailAddress).append('\'');
sb.append(", mPhoneNumber='").append(mPhoneNumber).append('\'');
sb.append(", mCarRegistrationNumber='").append(mCarRegistrationNumber).append('\'');
sb.append(", mLanguageCode='").append(mLanguageCode).append('\'');
sb.append(", mCountryCode='").append(mCountryCode).append('\'');
sb.append(", mHasBankingInformation=").append(mHasBankingInformation);
sb.append('}');
return sb.toString();
}
}
这是LoginResponse类:
public class LoginResponse extends WebServiceResponse<User> {
@Override
public void setResult(User result) {
super.setResult(result);
}
}
WebServiceResponse类:
public abstract class WebServiceResponse<T> {
// ===============================================================================
// Member variables
// ===============================================================================
@SerializedName("status")
private Status mStatus;
@SerializedName("result")
private T mResult;
// ===============================================================================
// Utility methods
// ===============================================================================
@Override
public String toString() {
return "WebServiceResponse{" +
"mStatus=" + mStatus +
", mResult=" + mResult +
'}';
}
/**
* @return Whether the response is valid, based on its status code.
*/
public boolean isValid() {
// TODO: Use a custom Error class
return mStatus != null && mStatus.getCode() == 0;
}
// ===============================================================================
// Accessors
// ===============================================================================
public Status getStatus() {
return mStatus;
}
public void setStatus(Status status) {
mStatus = status;
}
public T getResult() {
return mResult;
}
public void setResult(T result) {
mResult = result;
}
public static class Status {
// ===============================================================================
// Member variables
// ===============================================================================
@SerializedName("code")
private int mCode;
@SerializedName("message")
private String mMessage;
// ===============================================================================
// Utility methods
// ===============================================================================
@Override
public String toString() {
return "Status{" +
"mCode=" + mCode +
", mMessage='" + mMessage + '\'' +
'}';
}
// ===============================================================================
// Accessors
// ===============================================================================
public int getCode() {
return mCode;
}
public void setCode(int code) {
mCode = code;
}
public String getMessage() {
return mMessage;
}
public void setMessage(String message) {
mMessage = message;
}
}
}
signIn功能是这样的:
public Call<LoginResponse> signIn(final String username, final String password, final ConnectionCallback callback) {
Call<LoginResponse> call = WebServiceHelper2.getInstance().signIn(username, password);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Response<LoginResponse> response, Retrofit retrofit) {
if (response.isSuccess()) {
if (response.body().isValid()) {
storeSession(username, password);
notifySessionStateChange(true);
if (callback != null) {
callback.onSuccess(response.body().getResult());
}
} else {
callback.onError(response.body().getStatus().getCode());
}
}
}
@Override
public void onFailure(Throwable t) {
t.printStackTrace();
}
});
return call;
}
我缺少什么想法?响应是好的,但在此之后,结果对象的所有字段都为空,就像我说的那样。
答案 0 :(得分:0)
这是因为我使用的通用类型T,如Bhargav所说。它实施的方式只是以这种形式阅读和回答:
{"status":{"code":0,"message":"Ok."},"result":{"id":29367,"first_name":"Pinch","last_name":"Test2","alias_exists":true}}
但我的回答是这样的:
{"status":{"code":0,"message":"Ok."},"result":{"user":{"id":29367,"first_name":"Pinch","last_name":"Test2","alias_exists":true}}}
我已经创建了一个像这样的UserParent类:
public class UserParent implements Parcelable {
@SerializedName("user")
private User user;
protected UserParent(Parcel in) {
user = in.readParcelable(User.class.getClassLoader());
}
public static final Creator<UserParent> CREATOR = new Creator<UserParent>() {
@Override
public UserParent createFromParcel(Parcel in) {
return new UserParent(in);
}
@Override
public UserParent[] newArray(int size) {
return new UserParent[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(user, flags);
}
public User getChild() {
return user;
}
}
我实现了这样:
public class LoginResponse extends WebServiceResponse<UserParent> {
@Override
public void setResult(UserParent result) {
super.setResult(result);
}
@Override
public UserParent getResult() {
return super.getResult();
}
}