首页 > 解决方案 > 出 onResponse 结果 NULL

问题描述

所以事情就是这样,我从 JSON 获取数据,onResponse 内部一切正常,但外部返回 null,ps:Constants.idcategory 是静态变量,当我在 onResponse 方法中使用 AlertDialog 时,它几乎可以正常工作,但我需要在 onResponse 之外使用它,提前谢谢你

private void idCategory(String ip){
        Retrofit retrofit = new Retrofit.Builder().baseUrl(getString(R.string.login)).addConverterFactory(GsonConverterFactory.create()).build();
        JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
        Call<List<ListeCategories>> call = jsonPlaceHolderApi.getIdCategory(ip);
        call.enqueue(new Callback<List<ListeCategories>>() {
            @Override
            public void onResponse(Call<List<ListeCategories>> call, Response<List<ListeCategories>> response) {
                final List<ListeCategories> chaines = response.body();
                for(ListeCategories chaine:chaines){
                    mots = chaine.getLiveTV().split(",");
                    for (int i = 0; i < mots.length; i++) {
                        Constants.idcategory.add(Integer.parseInt(mots[i]));

                    }
                    break;
                }

            }

            @Override
            public void onFailure(Call<List<ListeCategories>> call, Throwable t) {

            }
        });

        String message = String.valueOf(Constants.idcategory.size());
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage(message)
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        //do things
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();



    }

标签: javaandroidjson

解决方案


body外部的数据onResponse()为 null,因为该方法是异步的。该对话框在响应到达之前显示,因此有空列表。

您应该将显示代码的对话框包装在某个方法中,并从onResponse()正文中调用此方法。请注意,该onResponse()方法是在后台线程上执行的,也许您需要主线程来显示对话框。

private void idCategory(String ip){
    Retrofit retrofit = new Retrofit.Builder().baseUrl(getString(R.string.login)).addConverterFactory(GsonConverterFactory.create()).build();
    JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
    Call<List<ListeCategories>> call = jsonPlaceHolderApi.getIdCategory(ip);
    call.enqueue(new Callback<List<ListeCategories>>() {
        @Override
        public void onResponse(Call<List<ListeCategories>> call, Response<List<ListeCategories>> response) {
            final List<ListeCategories> chaines = response.body();
            for(ListeCategories chaine:chaines){
                mots = chaine.getLiveTV().split(",");
                for (int i = 0; i < mots.length; i++) {
                    Constants.idcategory.add(Integer.parseInt(mots[i]));

                }
                break;
            }
            showDialog()
        }

        @Override
        public void onFailure(Call<List<ListeCategories>> call, Throwable t) {

        }
    });

private void showDialog() {
    String message = String.valueOf(Constants.idcategory.size());
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setMessage(message)
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //do things
                }
            });
    AlertDialog alert = builder.create();
    alert.show();



}

推荐阅读