Question

I have a problem with the my code using the Gson library.

When I run I get the following error:

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

Is there a way to get more detail about the error?

This is my json that I get:

{"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}

And this are the classes that parse the json:

The Response class:

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;
}
}

The profile class:

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;
}
}

Response classe that catches the response:

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;
}
Était-ce utile?

La solution

Found my problem: He didn't know the DateTime type from Joda-Datetime library. So had to register a TypeAdapter for that type.

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();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top