Java 响应结果为空

Java 响应结果为空,java,android,json,Java,Android,Json,问题是,我从JSON获得了数据,onResponse内部一切正常,但外部返回null,ps:Constants.idcategory是静态变量,当我在onResponse方法内部使用AlertDialog时,它几乎正常工作,但我需要在onResponse外部使用它,提前谢谢 private void idCategory(String ip){ Retrofit retrofit = new Retrofit.Builder().baseUrl(getString(R.strin

问题是,我从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();



    }
private void idCategory(字符串ip){
改型改型=新建改型.Builder().baseUrl(getString(R.string.login)).addConverterFactory(GsonConverterFactory.create()).build();
JsonPlaceHolderApi JsonPlaceHolderApi=reformation.create(JsonPlaceHolderApi.class);
Call Call=jsonPlaceHolderApi.getIdCategory(ip);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
最终列表链=response.body();
用于(列表类别链:链){
mots=chaine.getLiveTV().split(“,”);
对于(int i=0;i
由于方法是异步的,所以
onResponse()
正文之外的数据为空。对话框在响应到达之前显示,因此存在空列表

您应该用某种方法包装显示代码的对话框,并从
onResponse()
body调用此方法。请注意,
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();



}
private void idCategory(字符串ip){
改型改型=新建改型.Builder().baseUrl(getString(R.string.login)).addConverterFactory(GsonConverterFactory.create()).build();
JsonPlaceHolderApi JsonPlaceHolderApi=reformation.create(JsonPlaceHolderApi.class);
Call Call=jsonPlaceHolderApi.getIdCategory(ip);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
最终列表链=response.body();
用于(列表类别链:链){
mots=chaine.getLiveTV().split(“,”);
对于(int i=0;i
“…但我需要在onResponse之外使用它。”–为什么?确切地说,如果只是为了整洁起见,您可以将它放在一个单独的方法中,该方法从
onResponse()
调用,但无论放在何处,直到
onResponse()才能调用它
runs.maybe对您有用:)我需要在另一个活动中使用Constants.idcategory,但谢谢,我刚刚得到答案。我正在寻找答案,它是关于背景的,非常有用