使用改造未从 IGDB Api 接收所有数据
Not receiving all data from IGDB Api using retrofit
我正在使用 IGDB Api 构建应用程序以获取要在应用程序中显示的游戏列表。我也在使用改造库。目前我正在尝试让游戏封面显示在 GridView 中,但由于某种原因,当我要求封面 URL 以及除 [=34 之外的其他数据时,我从 Api 中得到 null =].任何人都可以帮我找出为什么我会变得空吗?提前致谢!
编辑:我认为从 IGDB 获取数据可能存在问题。我意识到当我在 Call<ArrayList<Game>> call = service.getAllGames(FIELDS);
中设置一个空字符串时,我得到了相同的结果,我只得到了 ID。虽然不知道为什么:/
Game.java:
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Game {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("cover")
@Expose
private Cover cover;
@SerializedName("name")
@Expose
private String name;
@SerializedName("popularity")
@Expose
private Double popularity;
@SerializedName("summary")
@Expose
private String summary;
@SerializedName("genres")
@Expose
private List<Genre> genres = null;
@SerializedName("platforms")
@Expose
private List<Platform> platforms = null;
@SerializedName("rating")
@Expose
private Double rating;
@SerializedName("release_dates")
@Expose
private List<ReleaseDate> releaseDates = null;
@SerializedName("videos")
@Expose
private List<Video> videos = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Cover getCover() {
return cover;
}
public void setCover(Cover cover) {
this.cover = cover;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPopularity() {
return popularity;
}
public void setPopularity(Double popularity) {
this.popularity = popularity;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public List<Genre> getGenres() {
return genres;
}
public void setGenres(List<Genre> genres) {
this.genres = genres;
}
public List<Platform> getPlatforms() {
return platforms;
}
public void setPlatforms(List<Platform> platforms) {
this.platforms = platforms;
}
public Double getRating() {
return rating;
}
public void setRating(Double rating) {
this.rating = rating;
}
public List<ReleaseDate> getReleaseDates() {
return releaseDates;
}
public void setReleaseDates(List<ReleaseDate> releaseDates) {
this.releaseDates = releaseDates;
}
public List<Video> getVideos() {
return videos;
}
public void setVideos(List<Video> videos) {
this.videos = videos;
}
}
Cover.java:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Cover {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("url")
@Expose
private String url;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
GameAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.riceplant.capstoneproject.R;
import com.riceplant.capstoneproject.data.Game;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameAdapterViewHolder> {
private ArrayList<Game> mGameData;
private Context mContext;
public GameAdapter(Context context, ArrayList<Game> gameData) {
mContext = context;
mGameData = gameData;
}
public class GameAdapterViewHolder extends RecyclerView.ViewHolder {
ImageView mGameCover;
public GameAdapterViewHolder(@NonNull View itemView) {
super(itemView);
mGameCover = itemView.findViewById(R.id.game_cover_image);
}
}
@NonNull
@Override
public GameAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutIdForListItem = R.layout.game_list_item;
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(layoutIdForListItem, parent, false);
return new GameAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull GameAdapterViewHolder holder, int position) {
Cover cover = mGameData.get(position).getCover();
String currentGame = null;
if (cover != null)
currentGame = cover.getUrl();
Picasso.get()
.load(currentGame)
.placeholder(R.drawable.image_loading)
.error(R.drawable.image_not_found)
.into(holder.mGameCover);
}
@Override
public int getItemCount() {
if (mGameData == null) {
return 0;
}
return mGameData.size();
}
}
PopularGamesFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.riceplant.capstoneproject.R;
import com.riceplant.capstoneproject.adapter.GameAdapter;
import com.riceplant.capstoneproject.data.Game;
import com.riceplant.capstoneproject.network.GameInstance;
import com.riceplant.capstoneproject.network.GetDataService;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class PopularGamesFragment extends Fragment {
private RecyclerView mRecyclerView;
private TextView mErrorTextMessage;
private GameAdapter mGameAdapter;
public static final String FIELDS = "fields name, platforms.name, cover.url, rating, release_dates.human, genres.name, summary, popularity, time_to_beat, videos.name, videos.video_id;";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_popular_games, container, false);
GetDataService service = GameInstance.getGameInstance().create(GetDataService.class);
Call<ArrayList<Game>> call = service.getAllGames(FIELDS);
call.enqueue(new Callback<ArrayList<Game>>() {
@Override
public void onResponse(Call<ArrayList<Game>> call, Response<ArrayList<Game>> response) {
mRecyclerView = view.findViewById(R.id.recycler_view);
generateDataList(response.body());
}
@Override
public void onFailure(Call<ArrayList<Game>> call, Throwable t) {
mErrorTextMessage = view.findViewById(R.id.popular_games_error_message);
mErrorTextMessage.setText("Something went wrong. Try again!");
}
});
return view;
}
private void generateDataList(ArrayList<Game> gameList) {
mGameAdapter = new GameAdapter(getActivity(), gameList);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mGameAdapter);
}
}
GameInstance.java
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class GameInstance {
private static Retrofit retrofit;
public static final String BASE_URL = "https://api-v3.igdb.com/";
public static Retrofit getGameInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
获取数据服务
import com.riceplant.capstoneproject.data.Game;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;
public interface GetDataService {
@Headers("user-key: GET YOU DATA KEY FROM IGDB")
@POST("games")
Call<ArrayList<Game>> getAllGames(@Body String fields);
}
您缺少 fields
正文 - GetDataService
上的参数。
文档 (https://api-docs.igdb.com/?kotlin#game) 指出您应该始终放入字段主体,以过滤要显示的字段。
我建议首先通过使用 Postman 或 curl 开始获得正确的 REST 请求:首先使用该工具形成请求,以便响应符合您的期望,然后才开始创建实际的应用程序实现。
所以步骤是:使用 /games 端点请求所有游戏,将所有字段指定为 POST 请求的主体。获得封面 ID 后,请求 /covers 并过滤您实际想要显示的封面,afaik。
祝你好运。
我正在使用 IGDB Api 构建应用程序以获取要在应用程序中显示的游戏列表。我也在使用改造库。目前我正在尝试让游戏封面显示在 GridView 中,但由于某种原因,当我要求封面 URL 以及除 [=34 之外的其他数据时,我从 Api 中得到 null =].任何人都可以帮我找出为什么我会变得空吗?提前致谢!
编辑:我认为从 IGDB 获取数据可能存在问题。我意识到当我在 Call<ArrayList<Game>> call = service.getAllGames(FIELDS);
中设置一个空字符串时,我得到了相同的结果,我只得到了 ID。虽然不知道为什么:/
Game.java:
import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Game {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("cover")
@Expose
private Cover cover;
@SerializedName("name")
@Expose
private String name;
@SerializedName("popularity")
@Expose
private Double popularity;
@SerializedName("summary")
@Expose
private String summary;
@SerializedName("genres")
@Expose
private List<Genre> genres = null;
@SerializedName("platforms")
@Expose
private List<Platform> platforms = null;
@SerializedName("rating")
@Expose
private Double rating;
@SerializedName("release_dates")
@Expose
private List<ReleaseDate> releaseDates = null;
@SerializedName("videos")
@Expose
private List<Video> videos = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Cover getCover() {
return cover;
}
public void setCover(Cover cover) {
this.cover = cover;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPopularity() {
return popularity;
}
public void setPopularity(Double popularity) {
this.popularity = popularity;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public List<Genre> getGenres() {
return genres;
}
public void setGenres(List<Genre> genres) {
this.genres = genres;
}
public List<Platform> getPlatforms() {
return platforms;
}
public void setPlatforms(List<Platform> platforms) {
this.platforms = platforms;
}
public Double getRating() {
return rating;
}
public void setRating(Double rating) {
this.rating = rating;
}
public List<ReleaseDate> getReleaseDates() {
return releaseDates;
}
public void setReleaseDates(List<ReleaseDate> releaseDates) {
this.releaseDates = releaseDates;
}
public List<Video> getVideos() {
return videos;
}
public void setVideos(List<Video> videos) {
this.videos = videos;
}
}
Cover.java:
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Cover {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("url")
@Expose
private String url;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
GameAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.riceplant.capstoneproject.R;
import com.riceplant.capstoneproject.data.Game;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class GameAdapter extends RecyclerView.Adapter<GameAdapter.GameAdapterViewHolder> {
private ArrayList<Game> mGameData;
private Context mContext;
public GameAdapter(Context context, ArrayList<Game> gameData) {
mContext = context;
mGameData = gameData;
}
public class GameAdapterViewHolder extends RecyclerView.ViewHolder {
ImageView mGameCover;
public GameAdapterViewHolder(@NonNull View itemView) {
super(itemView);
mGameCover = itemView.findViewById(R.id.game_cover_image);
}
}
@NonNull
@Override
public GameAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
int layoutIdForListItem = R.layout.game_list_item;
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(layoutIdForListItem, parent, false);
return new GameAdapterViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull GameAdapterViewHolder holder, int position) {
Cover cover = mGameData.get(position).getCover();
String currentGame = null;
if (cover != null)
currentGame = cover.getUrl();
Picasso.get()
.load(currentGame)
.placeholder(R.drawable.image_loading)
.error(R.drawable.image_not_found)
.into(holder.mGameCover);
}
@Override
public int getItemCount() {
if (mGameData == null) {
return 0;
}
return mGameData.size();
}
}
PopularGamesFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.riceplant.capstoneproject.R;
import com.riceplant.capstoneproject.adapter.GameAdapter;
import com.riceplant.capstoneproject.data.Game;
import com.riceplant.capstoneproject.network.GameInstance;
import com.riceplant.capstoneproject.network.GetDataService;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class PopularGamesFragment extends Fragment {
private RecyclerView mRecyclerView;
private TextView mErrorTextMessage;
private GameAdapter mGameAdapter;
public static final String FIELDS = "fields name, platforms.name, cover.url, rating, release_dates.human, genres.name, summary, popularity, time_to_beat, videos.name, videos.video_id;";
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_popular_games, container, false);
GetDataService service = GameInstance.getGameInstance().create(GetDataService.class);
Call<ArrayList<Game>> call = service.getAllGames(FIELDS);
call.enqueue(new Callback<ArrayList<Game>>() {
@Override
public void onResponse(Call<ArrayList<Game>> call, Response<ArrayList<Game>> response) {
mRecyclerView = view.findViewById(R.id.recycler_view);
generateDataList(response.body());
}
@Override
public void onFailure(Call<ArrayList<Game>> call, Throwable t) {
mErrorTextMessage = view.findViewById(R.id.popular_games_error_message);
mErrorTextMessage.setText("Something went wrong. Try again!");
}
});
return view;
}
private void generateDataList(ArrayList<Game> gameList) {
mGameAdapter = new GameAdapter(getActivity(), gameList);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(gridLayoutManager);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setAdapter(mGameAdapter);
}
}
GameInstance.java
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class GameInstance {
private static Retrofit retrofit;
public static final String BASE_URL = "https://api-v3.igdb.com/";
public static Retrofit getGameInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
获取数据服务
import com.riceplant.capstoneproject.data.Game;
import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;
public interface GetDataService {
@Headers("user-key: GET YOU DATA KEY FROM IGDB")
@POST("games")
Call<ArrayList<Game>> getAllGames(@Body String fields);
}
您缺少 fields
正文 - GetDataService
上的参数。
文档 (https://api-docs.igdb.com/?kotlin#game) 指出您应该始终放入字段主体,以过滤要显示的字段。
我建议首先通过使用 Postman 或 curl 开始获得正确的 REST 请求:首先使用该工具形成请求,以便响应符合您的期望,然后才开始创建实际的应用程序实现。
所以步骤是:使用 /games 端点请求所有游戏,将所有字段指定为 POST 请求的主体。获得封面 ID 后,请求 /covers 并过滤您实际想要显示的封面,afaik。
祝你好运。