亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

使用 retrofit2 在響應體上獲取空數據?

使用 retrofit2 在響應體上獲取空數據?

藍山帝景 2022-12-15 17:09:07
我是 retrofit 2 的新手,我正在嘗試獲取 json 數組數據,但結果為空,這是獲取數據的 url。但有趣的是,當我嘗試在內部進行調試時,onResponse我得到了成功消息和response.body數據。這里有什么問題?https://xenzet.com/GameCheat/viewgamesjson.php這是json數據[    {        "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"    }]這是試圖獲取數據的方法 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貢獻1815條經驗 獲得超6個贊

在 onResponse 方法中的適配器中添加數據


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

            }

        }


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

TA貢獻1784條經驗 獲得超7個贊

我沒有檢查你在哪里弄錯了。但是此代碼實現返回列表中包含 10 個數據的列表。


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 調用:


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

            }

        });

    }


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

TA貢獻1824條經驗 獲得超5個贊

問題是,您在 onCreate() 中設置了列表適配器,其中gameslist仍然為空。

adapter = new GameRecyclerViewAdapter(this, gameslist);
recyclerView.setAdapter(adapter);

當你調用 setAdapter() 方法時,recyclerView 會向適配器詢問項目的數量;我想在 getItemCount() 中,你有類似 return gameslist.count() 的東西。由于游戲列表仍然為空,這會導致 NPE。

要修復它,請將 getItemCount() 更改為如下所示:

return gameslist == null ? 0 : gameslist.size()

編輯

我檢查了您的 URL(使用 Postman),似乎響應的內容類型text/htmlapplication/json正確。我想因此沒有使用 Gson 轉換響應,因此您最終得到 null。


查看完整回答
反對 回復 2022-12-15
  • 3 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號