android - NPE with retrofit and nested JSON response -
i using retrofit 1.9 , i'm having trouble getting values nested json response. here response
{ "access_token": "83ebeabdec09f6670863766f792ead24d61fe3f9", "athlete": { "id": 227615, "resource_state": 3, ... } }
i have class handle response:
public class authresponse { string accesstoken; stravaathlete stravaathlete; public authresponse(string accesstoken, stravaathlete stravaathlete) { this.accesstoken = accesstoken; this.stravaathlete = stravaathlete; } public string getaccesstoken() { return accesstoken; } public stravaathlete getstravaathlete() { return stravaathlete; } }
and class nested json object:
public class stravaathlete { string id; public stravaathlete(string id) { this.id = id; } public string getid() { return id; } }
unfortunately whenever call authresponse.getstravaathlete().getid()
java.lang.nullpointerexception: attempt invoke virtual method 'java.lang.string com.drdp.rideart.api.model.stravaathlete.getid()' on null object reference
not sure doing wrong. have retrofit logging set full , can verify information in response in format expect
accesstoken
, stravaathlete
not key of json. that's why getting null. can either change them access_token
, athlete
, or use @serializedname
annotation. e.g.
public class authresponse { @serializedname("access_token") string accesstoken; @serializedname("athlete") stravaathlete stravaathlete;
Comments
Post a Comment