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

时间:2017-06-18 15:57:15

标签: java android retrofit2

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

布尔值显示错误以将其声明为最终

  

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

然后每次都返回false。 回应是

  

{“status”:“1”,“text”:“OTP successfull”}

1 个答案:

答案 0 :(得分:2)

您正在进行异步调用。仅在呼叫结束时才会执行onResponse方法。您的return success语句在此之前很久就会执行,因此返回false