简体   繁体   English

如何从Retrofit onResponse方法更改布尔值

[英]How to change the Boolean value from Retrofit onResponse method

 public Boolean getOtp(String mobile, String otp, String accessToken){
    Boolean success = false;

    progress.setVisibility(View.VISIBLE);
    progress.animate();
     Retrofit retrofit = new Retrofit.Builder()
             .baseUrl(getResources().getString(R.string.baseURL))
             .addConverterFactory(GsonConverterFactory.create())
             .build();
    APIService apiService = retrofit.create(APIService.class);
    Call<statusResponse> call = apiService.checkOtp(mobile,accessToken , otp);
    call.enqueue(new Callback<statusResponse>() {
        @Override
        public void onResponse(Response<statusResponse> response, Retrofit retrofit) {
            progress.setVisibility(View.INVISIBLE);
            statusResponse sp = response.body();

            if (sp.getStatus().equals("1")) {
                success = true;
                Log.e("Response ", sp.getText() + valueOf(success);
            } else {

                Log.e("Response Failed", String.valueOf(sp));
                /*Snackbar.make(cl, "Failed to update, please try again", Snackbar.LENGTH_LONG).show();*/
            }
            }

        @Override
        public void onFailure(Throwable t) {

        }
    });
    return success;
}

Boolean was showing error to declare it as final 布尔值显示错误,将其声明为最终值

final Boolean[] success = {false}; final Boolean [] success = {false}; return success[0] 成功返回[0]

Then it is returning false every time. 然后它每次都返回false。 response is 回应是

{"status":"1","text":"OTP successfull"} {“状态”:“ 1”,“文本”:“ OTP成功”}

You are making an asynchronous call. 您正在进行异步调用。 The onResponse method will be executed only when the call is finished. 仅在调用完成时才执行onResponse方法。 Your return success statement is executed long before that and therefore returns false . 您的return success语句在此之前执行很久,因此返回false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM