3 回答

TA貢獻(xiàn)1815條經(jīng)驗 獲得超6個贊
在 onResponse 方法中的適配器中添加數(shù)據(jù)
fetch_information()在oncreate中添加
@Override
public void onResponse(Call<List<Games>> call, Response<List<Games>> response) {
if(response.isSuccessful()) {
gameslist = response.body();
adapter = new GameRecyclerViewAdapter(MainActivity.this, gameslist);
//change your Activity name here insted of MainActivity
recyclerView.setAdapter(adapter);
}
}

TA貢獻(xiàn)1784條經(jīng)驗 獲得超7個贊
我沒有檢查你在哪里弄錯了。但是此代碼實現(xiàn)返回列表中包含 10 個數(shù)據(jù)的列表。
public interface APIInterface {
@GET()
Call<List<ViewGame>> viewgamesjson(@Url String url);
}
這是模型類:
public class ViewGame {
String gameid,gameIcon,Games_Name;
public String getGameid() {
return gameid;
}
public void setGameid(String gameid) {
this.gameid = gameid;
}
public String getGameIcon() {
return gameIcon;
}
public void setGameIcon(String gameIcon) {
this.gameIcon = gameIcon;
}
public String getGames_Name() {
return Games_Name;
}
public void setGames_Name(String games_Name) {
Games_Name = games_Name;
}
}
這是 APiClient 類:
public class APIClient {
static Retrofit retrofit = null;
public static String Base_URL = "https://xenzet.com/GameCheat/";
public static APIInterface getInterface() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder()
.readTimeout(60, TimeUnit.SECONDS)
.connectTimeout(60, TimeUnit.SECONDS)
.addInterceptor(interceptor)
.build();
retrofit = new Retrofit.Builder()
.baseUrl(Base_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit.create(APIInterface.class);
}
}
這是 API 調(diào)用:
private void viewGame() {
Call<List<ViewGame>> call = APIClient.getInterface().viewgamesjson("https://xenzet.com/GameCheat/viewgamesjson.php");
call.enqueue(new Callback<List<ViewGame>>() {
@Override
public void onResponse(Call<List<ViewGame>> call, Response<List<ViewGame>> response) {
try {
Global.dismissProgress();
if (response.isSuccessful() && response.body() != null) {
Global.dismissProgress();
Global.printLog("size===", response.body().size() + "");
} else {
Global.dismissProgress();
}
} catch (Exception e) {
e.printStackTrace();
Global.dismissProgress();
}
}
@Override
public void onFailure(Call<List<ViewGame>> call, Throwable t) {
Global.dismissProgress();
try {
Global.showToast(SplashActivity.this, getString(R.string.something_wrong));
} catch (Exception e) {
e.printStackTrace();
}
call.cancel();
t.printStackTrace();
}
});
}

TA貢獻(xiàn)1824條經(jīng)驗 獲得超5個贊
問題是,您在 onCreate() 中設(shè)置了列表適配器,其中gameslist
仍然為空。
adapter = new GameRecyclerViewAdapter(this, gameslist); recyclerView.setAdapter(adapter);
當(dāng)你調(diào)用 setAdapter() 方法時,recyclerView 會向適配器詢問項目的數(shù)量;我想在 getItemCount() 中,你有類似 return gameslist.count() 的東西。由于游戲列表仍然為空,這會導(dǎo)致 NPE。
要修復(fù)它,請將 getItemCount() 更改為如下所示:
return gameslist == null ? 0 : gameslist.size()
編輯
我檢查了您的 URL(使用 Postman),似乎響應(yīng)的內(nèi)容類型text/html
不application/json
正確。我想因此沒有使用 Gson 轉(zhuǎn)換響應(yīng),因此您最終得到 null。
添加回答
舉報