第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 retrofit2 在響應(yīng)體上獲取空數(shù)據(jù)?

使用 retrofit2 在響應(yīng)體上獲取空數(shù)據(jù)?

藍(lán)山帝景 2022-12-15 17:09:07
我是 retrofit 2 的新手,我正在嘗試獲取 json 數(shù)組數(shù)據(jù),但結(jié)果為空,這是獲取數(shù)據(jù)的 url。但有趣的是,當(dāng)我嘗試在內(nèi)部進(jìn)行調(diào)試時,onResponse我得到了成功消息和response.body數(shù)據(jù)。這里有什么問題?https://xenzet.com/GameCheat/viewgamesjson.php這是json數(shù)據(jù)[    {        "gameid":"2",        "gameIcon":"https:\/\/xenzet.com\/GameCheat\/uploads\/counter strike 1.6.png",        "Games_Name":"1"    },    {        "gameid":"3",        "gameIcon":"https:\/\/xenzet.com\/GameCheat\/uploads\/gta.ico",        "Games_Name":"vice city sucjlfsdrgefdsrhag"    },    {        "gameid":"4",        "gameIcon":"https:\/\/xenzet.com\/GameCheat\/uploads\/pubg.png",        "Games_Name":"pubg"    },    {    "gameid":"5",    "gameIcon":"https:\/\/xenzet.com\/GameCheat\/uploads\/doom.png",    "Games_Name":"Doom Enternal"    },    {    "gameid":"6",    "gameIcon":"https:\/\/xenzet.com\/GameCheat\/uploads\/for.png",    "Games_Name":"Fornite"    },    {    "gameid":"9",    "gameIcon":"https:\/\/xenzet.com\/GameCheat\/uploads\/dota2.png",    "Games_Name":"dota2"    }]這是試圖獲取數(shù)據(jù)的方法 public void fetch_information() {        ApiInterface = ApiClient.getApiClient().create(Api.class);        Call<List<Games>> call = ApiInterface.GetGames();        call.enqueue(new Callback<List<Games>>() {            @Override            public void onResponse(Call<List<Games>> call, Response<List<Games>> response) {                if(response.isSuccessful()) {                    gameslist = response.body();                    gamescounter = gameslist.size();                    adapter = new GameRecyclerViewAdapter(GamesActivity.this, gameslist);                    recyclerView.setAdapter(adapter);                }            }            @Override            public void onFailure(Call<List<Games>> call, Throwable t) {                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();            }        });    }
查看完整描述

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

            }

        }


查看完整回答
反對 回復(fù) 2022-12-15
?
嚕嚕噠

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();

            }

        });

    }


查看完整回答
反對 回復(fù) 2022-12-15
?
滄海一幻覺

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/htmlapplication/json正確。我想因此沒有使用 Gson 轉(zhuǎn)換響應(yīng),因此您最終得到 null。


查看完整回答
反對 回復(fù) 2022-12-15
  • 3 回答
  • 0 關(guān)注
  • 156 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號