2017-10-15 1 views
0

J'utilise retorfit pour récupérer des données de l'API Steam, mais ma réponse est null, mais la journalisation affiche la réponse JSON correcte.La réponse de rattrapage est nulle

C'est le JSON de l'API:

{ 
"response": { 
    "players": [ 
     { 
      "steamid": "76561197960435530", 
      "communityvisibilitystate": 3, 
      "profilestate": 1, 
      "personaname": "Robin", 
      "lastlogoff": 1508048552, 
      "profileurl": "http://steamcommunity.com/id/robinwalker/", 
      "avatar": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4.jpg", 
      "avatarmedium": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_medium.jpg", 
      "avatarfull": "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/f1/f1dd60a188883caf82d0cbfccfe6aba0af1732d4_full.jpg", 
      "personastate": 0, 
      "realname": "Robin Walker", 
      "primaryclanid": "103582791429521412", 
      "timecreated": 1063407589, 
      "personastateflags": 0, 
      "loccountrycode": "US", 
      "locstatecode": "WA", 
      "loccityid": 3961 
     } 
    ] 
} 

}

Ce sont mes POJO:

public class Response { 

@SerializedName("players") 
private List<Player> mPlayers; 

public List<Player> getPlayers() { 
    return mPlayers; 
} 

public void setPlayers(List<Player> players) { 
    mPlayers = players; 
} 

}

public class Player { 

@SerializedName("avatar") 
private String mAvatar; 
@SerializedName("avatarfull") 
private String mAvatarfull; 
@SerializedName("avatarmedium") 
private String mAvatarmedium; 
@SerializedName("communityvisibilitystate") 
private Long mCommunityvisibilitystate; 
@SerializedName("lastlogoff") 
private Long mLastlogoff; 
@SerializedName("loccityid") 
private Long mLoccityid; 
@SerializedName("loccountrycode") 
private String mLoccountrycode; 
@SerializedName("locstatecode") 
private String mLocstatecode; 
@SerializedName("personaname") 
private String mPersonaname; 
@SerializedName("personastate") 
private Long mPersonastate; 
@SerializedName("personastateflags") 
private Long mPersonastateflags; 
@SerializedName("primaryclanid") 
private String mPrimaryclanid; 
@SerializedName("profilestate") 
private Long mProfilestate; 
@SerializedName("profileurl") 
private String mProfileurl; 
@SerializedName("realname") 
private String mRealname; 
@SerializedName("response") 
private Response mResponse; 
@SerializedName("steamid") 
private String mSteamid; 
@SerializedName("timecreated") 
private Long mTimecreated; 

public String getAvatar() { 
    return mAvatar; 
} 

public void setAvatar(String avatar) { 
    mAvatar = avatar; 
} 

public String getAvatarfull() { 
    return mAvatarfull; 
} 

public void setAvatarfull(String avatarfull) { 
    mAvatarfull = avatarfull; 
} 

public String getAvatarmedium() { 
    return mAvatarmedium; 
} 

public void setAvatarmedium(String avatarmedium) { 
    mAvatarmedium = avatarmedium; 
} 

public Long getCommunityvisibilitystate() { 
    return mCommunityvisibilitystate; 
} 

public void setCommunityvisibilitystate(Long communityvisibilitystate) { 
    mCommunityvisibilitystate = communityvisibilitystate; 
} 

public Long getLastlogoff() { 
    return mLastlogoff; 
} 

public void setLastlogoff(Long lastlogoff) { 
    mLastlogoff = lastlogoff; 
} 

public Long getLoccityid() { 
    return mLoccityid; 
} 

public void setLoccityid(Long loccityid) { 
    mLoccityid = loccityid; 
} 

public String getLoccountrycode() { 
    return mLoccountrycode; 
} 

public void setLoccountrycode(String loccountrycode) { 
    mLoccountrycode = loccountrycode; 
} 

public String getLocstatecode() { 
    return mLocstatecode; 
} 

public void setLocstatecode(String locstatecode) { 
    mLocstatecode = locstatecode; 
} 

public String getPersonaname() { 
    return mPersonaname; 
} 

public void setPersonaname(String personaname) { 
    mPersonaname = personaname; 
} 

public Long getPersonastate() { 
    return mPersonastate; 
} 

public void setPersonastate(Long personastate) { 
    mPersonastate = personastate; 
} 

public Long getPersonastateflags() { 
    return mPersonastateflags; 
} 

public void setPersonastateflags(Long personastateflags) { 
    mPersonastateflags = personastateflags; 
} 

public String getPrimaryclanid() { 
    return mPrimaryclanid; 
} 

public void setPrimaryclanid(String primaryclanid) { 
    mPrimaryclanid = primaryclanid; 
} 

public Long getProfilestate() { 
    return mProfilestate; 
} 

public void setProfilestate(Long profilestate) { 
    mProfilestate = profilestate; 
} 

public String getProfileurl() { 
    return mProfileurl; 
} 

public void setProfileurl(String profileurl) { 
    mProfileurl = profileurl; 
} 

public String getRealname() { 
    return mRealname; 
} 

public void setRealname(String realname) { 
    mRealname = realname; 
} 

public Response getResponse() { 
    return mResponse; 
} 

public void setResponse(Response response) { 
    mResponse = response; 
} 

public String getSteamid() { 
    return mSteamid; 
} 

public void setSteamid(String steamid) { 
    mSteamid = steamid; 
} 

public Long getTimecreated() { 
    return mTimecreated; 
} 

public void setTimecreated(Long timecreated) { 
    mTimecreated = timecreated; 
} 

}

Et voici ma demande:

call.enqueue(new Callback<Response>() { 
        @Override 
        public void onResponse(Call<Response> call, Response<Response> response) { 
         Log.d(TAG, "onResponse: " + response.body().getPlayers()); 

        } 

        @Override 
        public void onFailure(Call<com.example.konem.pubgstat.Models.Response> call, Throwable t) { 

        } 
       }); 

J'ai essayé quelques solution, mais je ne peux pas le faire fonctionner, les response.body.getPlayers() retourne toujours NULL. Merci pour votre aide :)

Répondre

0

J'ai eu problème avec la sérialisation, je manqué une classe Java, maintenant j'ai trois POJO et fonctionne parfaitement:

@SerializedName("response") 
@Expose 
private Response_ response; 

public Response_ getResponse() { 
    return response; 
} 

public void setResponse(Response_ response) { 
    this.response = response; 
} 



public class Response_ { 

@SerializedName("players") 
@Expose 
private List<Player> players = null; 

public List<Player> getPlayers() { 
    return players; 
} 

public void setPlayers(List<Player> players) { 
    this.players = players; 
} 

}

Et la classe des joueurs.

+0

S'il vous plaît, pouvez-vous copier et coller les journaux lorsque vous effectuez l'appel et la réponse en retour? probablement votre modèle de données est correct, mais vous recevez en réalité une réponse nulle. – DaCrAn

+0

Mon modèle de données était incorrect, j'ai raté un cours, la réponse était toujours bonne :) –