2017-10-15 182 views
0

我正在使用retorfit從Steam API獲取數據,但我的響應爲空,但日誌顯示正確的JSON響應。Retrofit響應爲空

這是從API的JSON:

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

}

這些都是我的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; 
} 

}

這裏是我的要求:

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) { 

        } 
       }); 

我嘗試了一些解決方法,但我不能讓它工作,response.body.getPlayers()總是返回null。 感謝您的幫助:)

回答

0

我有問題的系列化,我錯過了一個Java類,現在我有三個POJO和工作完美:

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

}

和球員類。

+0

拜託,你可以複製並粘貼日誌,當你打電話和迴應? 可能你的數據模型是正確的,但你實際上收到一個空響應。 – DaCrAn

+0

我的數據模型不正確,我錯過了一堂課,回覆總是很好:) –