3

My code is below and following this article to implement Recaptcha in Android Studio 3: https://developer.android.com/training/safetynet/recaptcha.html

btn_Login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View view) {
        SafetyNet.getClient(this).verifyWithRecaptcha("api key")
            .addOnSuccessListener((Executor) this,
                    new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
                        @Override
                        public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
                            String userResponseToken = response.getTokenResult();
                            if (!userResponseToken.isEmpty()) {
                            }
                        }
                    })
            .addOnFailureListener((Executor) this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    if (e instanceof ApiException) {
                        ApiException apiException = (ApiException) e;
                        int statusCode = apiException.getStatusCode();
                    } else {
                    }
                }
            });

    }
});

I am facing a compilation error below.

in-convertible types: cannot cast anonymous android.view.view.onclicklistener to java.util.concurrent.executor

Am I missing anything?

Pankaj
  • 9,749
  • 32
  • 139
  • 283

2 Answers2

5

I used below code and everything is work fine now.

Make sure to implement Executor in the activity

btn_Login.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(final View view) {
        SafetyNet.getClient(Activity.this).verifyWithRecaptcha("")
            .addOnSuccessListener((Activity) MyActivity.this,
                new OnSuccessListener<SafetyNetApi.RecaptchaTokenResponse>() {
                    @Override
                    public void onSuccess(SafetyNetApi.RecaptchaTokenResponse response) {
                        // Indicates communication with reCAPTCHA service was
                        // successful.
                        String userResponseToken = response.getTokenResult();
                        if (!userResponseToken.isEmpty()) {
                            // Validate the user response token using the
                            // reCAPTCHA siteverify API.
                        }
                    }
                })
            .addOnFailureListener((Activity) MyActivity.this, new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    if (e instanceof ApiException) {
                        // An error occurred when communicating with the
                        // reCAPTCHA service. Refer to the status code to
                        // handle the error appropriately.
                        ApiException apiException = (ApiException) e;
                        int statusCode = apiException.getStatusCode();

                    } else {
                    }
                }
            });
    }
});
Pankaj
  • 9,749
  • 32
  • 139
  • 283
2

The problem is this. In this case this is class View.OnClickListener, it's not an Activity.

You should fix like this SafetyNet.getClient(YourActivity.this) and (Executor) YourActivity.this

Nha Phạm Thị
  • 409
  • 4
  • 10