在onresponse方法之外使用api响应(2)

yuvru6vn  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(278)

伙计们,我正在使用api请求获取一个列表,并使用Reformation2进行响应。这是我的代码:

private void displayfollowup(String id,String sestoken) {
    followuplist=new ArrayList<>();

    Retrofit retrofit = RetrofitInstance.getRetrofitInstance();
    final Api api = retrofit.create(Api.class);
    Call<List<TraitementTicketModel>> call = api.getfollowup(id, sestoken);
    call.enqueue(new Callback<List<TraitementTicketModel>>() {

        @Override
        public void onResponse(Call<List<TraitementTicketModel>> call, Response<List<TraitementTicketModel>> response) {
            if (!response.isSuccessful()) {
                Toast.makeText(getApplicationContext(), "Something is wrong !! ", Toast.LENGTH_LONG).show();
                Log.e("TAG", "onResponse: something is wrong");

            } else if (response.body() == null) {

                return;
            }

            List<TraitementTicketModel> followups = response.body();

            for (TraitementTicketModel followup : followups) {

                followuplist.add(followup);

            }

        }

        @Override
        public void onFailure(Call<List<TraitementTicketModel>> call, Throwable t) {
            Toast.makeText(getApplicationContext(),"Pas de connextion internet",Toast.LENGTH_LONG).show();
        }
    });

}

我在这里的问题是,当我尝试在displayfollowup之外使用此列表(followuplist)时,onresponse方法我发现它为空,所以我尝试使onresponse重新运行列表而不是void,但它不起作用,我在“从2.callback派生的匿名类”中得到了此消息(onresponse(call,response)”,“2.回调”中的“响应”;尝试使用不兼容的返回类型)
我不知道该怎么办谢谢你的帮助

lnvxswe2

lnvxswe21#

您可以传递对象并使用以下代码执行操作:

TraitementTicketModel object  = response.body;

将此对象传递给方法或要使用数据集的位置。对象具有所有数据集。

相关问题