Laroussi Aziz
Laroussi Aziz

Reputation: 11

using api response outside of onResponse method (retrofit2)

guys i'm using api request to get a list on response using retrofit2 this is my code :

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


}

my issue here that when i try to use this list (followuplist) outside of displayfollowup,onResponse method I found it empty so I tried to make onResponse retrun list instead of void but it didn't work and I got this message (onResponse(Call<List>, Response<List>)' in 'Anonymous class derived from retrofit2.Callback' clashes with 'onResponse(Call, Response)' in 'retrofit2.Callback'; attempting to use incompatible return type)

i didn't know what to do appreciate anyhelp

Upvotes: 0

Views: 328

Answers (1)

AshishVE
AshishVE

Reputation: 216

You can pass the object and perform the operations with this code:

TraitementTicketModel object  = response.body;

Pass this object to your method or where you want to use the data set. Object has all the data set.

Upvotes: 0

Related Questions