PCcloob.Ir
PCcloob.Ir

Reputation: 97

define new String inside onResponse in retrofit

i use retrofit2. i'm trying define String "stat" inside onResponse but it return null.

public class StatusConfig  {    
        private String stat;    
        .......................
        public String Status() {
             APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
            Call<Login> call = apiInterface.LoginCheck();
            call.enqueue(new Callback<Login>() {
                @Override
                public void onResponse(Call<Login> call, Response<Login> response) {
                    if(response.isSuccessful()) stat="true";
                    else stat="false";
                }
                @Override
                public void onFailure(Call<Login> call, Throwable t) {
                }
            }); 
            return stat;
        }
    ............
    }

i'm trying this codes but still return null.

public class StatusConfig  {    
    .......................
    public String Status() {
       final String[] stat = new String[1];
         APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
        Call<Login> call = apiInterface.LoginCheck();
        call.enqueue(new Callback<Login>() {
            @Override
            public void onResponse(Call<Login> call, Response<Login> response) {
                if(response.isSuccessful()) stat[0]="true";
                else stat[0]="false";
            }
            @Override
            public void onFailure(Call<Login> call, Throwable t) {
            }
        }); 
        return stat[0];
    }
............
}

what is the problem? what is wrong?

Upvotes: 0

Views: 237

Answers (2)

Abu Yousuf
Abu Yousuf

Reputation: 6107

Calling call.enqueue() will be executed asynchronously. Whats happening here is you are returning stat immediately which is not initialized. After returning new Callback is executed and it's assigning value to stat .

You have to use interface to get notified when new Callback is has called.

Interface:

public interface OnResponseCallback{
   public void onGetResponse(String data);
}

Initialize a instance of that interface

 OnResponseCallback callBack = new  OnResponseCallback(){
     public void onGetResponse(String data){
       // your response is here  
     }    
 };

And pass callBack on Status method.

   public void Status(OnResponseCallback callBack) {
   final String[] stat = new String[1];
     APIInterface apiInterface = APIClient.getClient().create(APIInterface.class);
    Call<Login> call = apiInterface.LoginCheck();
    call.enqueue(new Callback<Login>() {
        @Override
        public void onResponse(Call<Login> call, Response<Login> response) {
            if(response.isSuccessful()) {
                 // stat[0]="true";
                 if(callBack != null) callBack.onGetResponse("true");
            }else{
                 if(callBack != null) callBack.onGetResponse("false");  
            }

            // else stat[0]="false";
        }
        @Override
        public void onFailure(Call<Login> call, Throwable t) {
        }
    }); 
   // return stat[0];
}

Call it this way

new StatusConfig().status(callBack)

Check this tutorial to understand callback using interface

Upvotes: 1

Juanjo Berenguer
Juanjo Berenguer

Reputation: 789

Ir.

This code is working fine, the problem is the logic. If you write a Toast inside the onResponse or onFailure you will see how the stat variable has value.But is null because the the enqueue method is asynchronous so the method Status always will return null because this method doesn't wait for the asynchronous execution.

I replied your code calling another Api service you can test it with your code:

public class StatusConfig  {
    private String stat;

    public String Status() {
        IpifyService apiService = initRetrofit();
        Call<ResponseBody> call = apiService.getIp();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                //When the response is successfull we will asign the value true to stat
                if(response.isSuccessful()){
                    stat="true";
                    Toast.makeText(getApplicationContext(),stat,Toast.LENGTH_SHORT).show();
                }
                //When the response is not successfull we will asign the value false to stat
                else{
                    stat="false";
                    Toast.makeText(getApplicationContext(),stat,Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                //When the the onFailure is called we will asign the value error to stat
                stat = "error";
                Toast.makeText(getApplicationContext(),stat,Toast.LENGTH_SHORT).show();
            }
        });

        //This always is null because the enqueue call is not executed yet.
        return stat;
    }
}

I hope that helps you to understand your code.

Upvotes: 0

Related Questions