为了账号安全,请及时绑定邮箱和手机立即绑定

使用 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 关注
  • 109 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信