我的代码使用Gson库时遇到问题。
当我跑步时,我收到以下错误:
5596-5596/be.appmax.ktsjjt E/AndroidRuntime﹕ FATAL EXCEPTION: main
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 128
有没有办法获得有关错误的更多细节?
这是我得到的json:
{"data":{"pro_email":"kevingoos@telenet.be","pro_last_name":"Goos","pro_sex":"M","pro_nickname":"Kevin Goos","pro_date_birth":"1996-04-23","pro_id":14,"pro_facebook_id":"1333567985","pro_first_name":"Kevin","pro_avatar":"http:\/\/graph.facebook.com\/1333567985\/picture","pro_date_updated":"2014-04-09 11:32:20"},"status":200}
这是解析json的类:
Response类:
public class ProfileResponse {
@SerializedName("status")
private int status;
@SerializedName("data")
private Profile data;
public ProfileResponse() {
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Profile getData() {
return data;
}
public void setData(Profile data) {
this.data = data;
}
}
个人资料类:
public class Profile implements Parcelable {
//ID
@SerializedName("pro_id")
private int id;
//FIELDS
@SerializedName("pro_nickname")
private String nickname;
@SerializedName("pro_email")
private String email;
@SerializedName("pro_sex")
private String sex;
//TIME OR DATE
@SerializedName("pro_date_birth")
private DateTime birth_day;
@SerializedName("pro_date_updated")
private DateTime updated;
@SerializedName("pro_avatar")
private String avatar;
@SerializedName("pro_first_name")
private String firstName;
@SerializedName("pro_last_name")
private String lastName;
@SerializedName("pro_facebook_id")
private String facebookId;
public Profile() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public DateTime getBirth_day() {
return birth_day;
}
public void setBirth_day(String date) {
this.birth_day = JodaTimeUtil.getDateTimeFromString(date);
}
public DateTime getUpdated() {
return updated;
}
public void setUpdated(String date) {
this.updated = JodaTimeUtil.getDateTimeFromString(date);
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
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 getFacebookId() {
return facebookId;
}
public void setFacebookId(String facebookId) {
this.facebookId = facebookId;
}
@Override
public String toString() {
return "Id: " + id + " - FBID: " + facebookId + " - NAME: " + nickname;
}
@Override
public int describeContents() {
return 0;
}
}
回应classe捕获响应:
public static Profile getProfile(JSONObject response) throws IOException {
Profile profile = null;
// Map JSON to JAVA Objects
Type ProfileResponse = new TypeToken<ObjectResponse<String>>(){}.getType();
ProfileResponse profileResponse = Shared.gson.fromJson(response.toString(), ProfileResponse.class);
if (profileResponse != null) {
// Status, Message, Data
int status = profileResponse.getStatus();
Tools.LOG_DEBUG("ResponseBeers - getAll, Status: " + status);
if (profileResponse.getData() != null) {
profile = profileResponse.getData();
}
}
return profile;
}
答案 0 :(得分:5)
发现我的问题:他不知道Joda-Datetime库中的DateTime类型。因此必须为该类型注册一个TypeAdapter。
Gson gson = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
@Override
public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return JodaTimeUtil.getDateTimeFromString(json.getAsString());
}
}).create();