1
這是當我使用改進將對象發送到服務器時出現問題的原因。我這樣做是爲了創建JSON對象:轉換爲json時對象變爲空
HashMap<String, UserModel> map = new HashMap<>();
map.put("user", user);
但是,當JSON在我得到了這樣的服務器到達:
{"user":null}
然後我印有該行紐約JSON文件:
Log.d("TAG", new JSONObject(map).toString());
而且我看到了同樣的空對象。
那麼,這是我的問題,爲什麼會發生這種情況?我該如何解決這個問題?
這裏去了解我的項目的一些信息:
Retrofit version: 2.0.0
Retrofit serializer: jackson version 2.0.0
using also jackson to convert JodaTime version 2.4.0
在這裏不用我如何得到改造實例:
public T buildServiceInstance(Class<T> clazz){
return new Retrofit.Builder().baseUrl(BuildConfig.API_HOST)
.addConverterFactory(JacksonConverterFactory.create())
.build().create(clazz);
}
我認爲這種方法在這裏:
public static final IUserApi serviceInstance = new ApiBuildRequester<IUserApi>()
.buildServiceInstance(IUserApi.class);
方法聲明在接口IUserApi上:
@POST("User.svc/Save")
Call<ResponseSaveUserApiModel> save(@Body HashMap<String, UserModel> map);
,最後,但我想,不是那麼重要:
public class UserModel implements Parcelable {
private String idUser;
private String name;
private String email;
@JsonProperty("password")
private String safePassword;
private String salt;
private String phoneNumber;
private String facebookProfilePictureUrl;
private String facebookUserId;
public UserModel() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIdUser() {
return idUser;
}
public void setIdUser(String idUser) {
this.idUser = idUser;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSafePassword() {
return safePassword;
}
public void setSafePassword(String safePassword) {
this.safePassword = safePassword;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getFacebookProfilePictureUrl() {
return facebookProfilePictureUrl;
}
public void setFacebookProfilePictureUrl(String facebookProfilePictureUrl) {
this.facebookProfilePictureUrl = facebookProfilePictureUrl;
}
public String getFacebookUserId() {
return facebookUserId;
}
public void setFacebookUserId(String facebookUserId) {
this.facebookUserId = facebookUserId;
}
@Override
public int describeContents() {
return 0;
}
public UserModel(Parcel in) { // Deve estar na mesma ordem do "writeToParcel"
setIdUser(in.readString());
setName(in.readString());
setEmail(in.readString());
setSafePassword(in.readString());
setPhoneNumber(in.readString());
setFacebookProfilePictureUrl(in.readString());
setFacebookUserId(in.readString());
}
@Override
public void writeToParcel(Parcel dest, int flags) { //Deve estar na mesma ordem do construtor que recebe parcel
dest.writeString(idUser);
dest.writeString(name);
dest.writeString(email);
dest.writeString(safePassword);
dest.writeString(phoneNumber);
dest.writeString(facebookProfilePictureUrl);
dest.writeString(facebookUserId);
}
public static final Parcelable.Creator<UserModel> CREATOR = new Parcelable.Creator<UserModel>(){
@Override
public UserModel createFromParcel(Parcel source) {
return new UserModel(source);
}
@Override
public UserModel[] newArray(int size) {
return new UserModel[size];
}
};
public String getSalt() {
return salt;
}
public void setSalt(String salt) {
this.salt = salt;
}
}
調試屏幕:
您確定您的用戶對象在序列化時不爲空嗎? –
@MikhailChibel是的,我調試過了,檢查屏幕截圖 – guisantogui
org.json(JSONObject)沒有mapper API。它不能將你的pojo翻譯成json。閱讀包裝文檔! – Selvin