1

I use Retrofit in my project. And i have problem. My JSON not convert to object.

Here it is my JSON:

{"id":1,"facebookId":"123","uuid":"xU71niPhrqzFqmBFhk5fl4LlvWg6zk42ENG4iMjgl3eJEb9LAcqVcc9NmLHzFIgg","userInfo":{"id":1,"user":null,"email":"IP696@mail.ru","firstName":"Pavel","lastName":"Petrashov","name":null,"dob":null,"gender":null,"facebookLink":"link","locale":null,"location":null,"timezone":null,"verified":null,"updatedTime":null},"balancedCustomer":null,"session":{"id":1,"session":"FTKuyeb1BXBzXRZzsAuwPYs4eRIpdi2Z","expirationDate":1424839445000,"ip":null,"user":null}}

Here it is my Retrofit method:

public ApiUser getApiUser(){
        if (null == apiUser) {
            Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
            RestAdapter.Builder builder = new RestAdapter.Builder()
                    .setEndpoint(BuildConfig.ROOT_URL_SKIP)
                    .setClient(new OkClient(new OkHttpClient()))
                    .setConverter(new GsonConverter(gson));
            builder.setLogLevel(RestAdapter.LogLevel.FULL);

            RestAdapter restAdapter = builder.build();
            apiUser = restAdapter.create(ApiUser.class);
        }
        return apiUser;
    }

and i get this error:

Error: com.google.gson.JsonSyntaxException: 1424839445000

My classes look like this:

@Table(name = "Users")
public class User extends Model {

    @Expose
    @Column(name = "facebookId")
    @SerializedName("facebookId")
    private String facebookId;

    @Expose
    @Column(name = "uuid")
    @SerializedName("uuid")
    private String uuid;

    @Expose
    @Column(name = "userInfo")
    @SerializedName("userInfo")
    private UserInfo userInfo;

    @Expose
    @Column(name = "balancedCustomer")
    @SerializedName("balancedCustomer")
    private BalancedCustomer balancedCustomer;

    @Expose
    @Column(name = "session")
    @SerializedName("session")
    private Session session;

    public User() {
        super();
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public String getFacebookId() {
        return facebookId;
    }

    public void setFacebookId(String facebookId) {
        this.facebookId = facebookId;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getUuid() {
        return uuid;
    }

    public BalancedCustomer getBalancedCustomer() {
        return balancedCustomer;
    }

    public void setBalancedCustomer(BalancedCustomer balancedCustomer) {
        this.balancedCustomer = balancedCustomer;
    }
}

edited, this whole class.

@Table(name = "Sessions")
public class Session extends Model {

    @Expose
    @Column(name = "session")
    @SerializedName("session")
    private String session;

    @Expose
    @Column(name = "expirationDate")
    @SerializedName("expirationDate")
    private Date expirationDate;

...

5 Answers5

1
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                    return new Date(json.getAsJsonPrimitive().getAsLong());
                }
            }).create();

thanks for the link @Deepak Bala enter link description here

Community
  • 1
  • 1
0

Don't try to get expirationDate with Date object because in json it is like unformatted.what you should do ,receive like string or double in model classes then inside application you can change or parse to date format.

    @Expose
    @Column(name = "expirationDate")
    @SerializedName("expirationDate")
    private String expirationDate;
learner
  • 3,092
  • 2
  • 21
  • 33
0

Get expirationDate as long instead of Date :

    @Expose
    @Column(name = "expirationDate")
    @SerializedName("expirationDate")
    private long expirationDate;
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

this will solve your problem : solution

Let me know if you face any problem with it.

Community
  • 1
  • 1
dvs
  • 644
  • 5
  • 14
0

Change the data type of the Session->expiryDate to Long or String then when u want to convert that to a Date object just use this.

Date date=new Date(expirationDate);
Gowtham Raj
  • 2,915
  • 1
  • 24
  • 38